Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6822189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:38:01+00:00 2026-05-26T21:38:01+00:00

I am new to taking an object oriented approach with php…actually I have a

  • 0

I am new to taking an object oriented approach with php…actually I have a lot of learning to do overall, but the only way to learn is by doing.

So I have an array that holds several DateTime objects..I know this to be true because I have ran var_dump on the array and it indeed is holding several objects.

I need to go through each date and make sure there is never a difference greater than one day.

My research would seem to indicate that we cannot access or modify an object using the subscript operator:

$foo = $neat_array[$i+1]->format('U') ; //looking to format DateTime object as unix

//this returns an error every time

Okay I am fine with that, but I simply cannot figure out the syntax to access a specific item in the array so that it is seen as an object and does not pull an error.

I have pieced together that using -> is how I need to do it, but I never get any useable result.

Here is pseudo code of what I am trying to do

foreach($date_array as $date)
{

    //check to see if the difference between the next date in the array and the current date of the array is greater than one day.

    //I cannot use diff because I am on php 5.2 so I am trying this

    $date->format('U') and then doing the math

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:38:01+00:00Added an answer on May 26, 2026 at 9:38 pm

    Since learning by experimenting (and failing) is IMO a great way to learn, here is something to get you started.

    $date_array = array(
      new DateTime("tomorrow"),
      new DateTime("now"),
      new DateTime("yesterday"),
      new DateTime("last day of october")
    );
    
    for( $i = 0, $count = count( $date_array); $i < $count; $i += 2) {
      // Get the current object AND the next one in the array
      echo $date_array[ $i ]->format('U'); echo '<br />';
      echo $date_array[ $i + 1 ]->format('U'); echo '<br />';
      // Now that you have the UNIX timestamps, you can do the math you need in here.
    }
    

    Note that the above will fail if the array contains an odd number of elements – I’ll leave that fix up to you (if you need it).

    Demo

    Edit: You’re probably getting that error for one of two reasons:

    1. You’re referencing an element that either isn’t a DateTime object
    2. You’re referencing an element that doesn’t exist in the date array

    Here is a demo that shows error #2.

    Edit: Here is a working example based off your answer that works for even and odd array sizes. It is essentially the same, except I don’t bother with saving the values to variables or subtracting and comparing to 1 (since it’s unnecessary).

    $session_dates = $date_array = array(
      new DateTime("tomorrow"),
      new DateTime("yesterday"),
      new DateTime("now"),
    );
    
    for( $i = 0, $count = count( $date_array) - 1; $i < $count; $i++) 
    {
      if(  ($date_array[ $i + 1 ]->format('U') - $date_array[ $i ]->format('U')) > 86400) 
      {
         die( 'The date array does not contain consecutive dates.');
      }
    }
    

    See it in action.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I believe I have been taking the right approach to this so far, but
I'm very new to Unix, and currently taking a class learning the basics of
I'm working through this tutorial: http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-3.php At first he has you create a setter
I have a bitmap object which is taking a huge part of the memory
Recently, while taking an introductory unit focused on Object Oriented programming, I was introduced
I have a new-ish rails 3.1.1 app using sass/compass with compass taking care of
A co-worker of mine is taking an Introduction to Object Oriented Programming class and
Background Information I'm currently taking a computer graphics class. The way we're learning the
I'm quite new to Flex. I've looked into taking screenshots in flex and have
I am looking at taking on a new web development project and the customer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.