-3

actually I have 2 DateTime objects

$d1 = new DateTime('04/14/2013 8.00 AM');
$d2 = new DateTime('04/14/2013');

so without doing any changes to these 2 objects. is it possible equal these 2 objects.

user2047701
  • 31
  • 1
  • 1
  • 7

3 Answers3

1

To get the difference between two DateTime objects, use DateTime::diff() method:

$interval = $d1->diff($d2);

As a result, you will get a DateInterval object. To get formatted time difference from the DateInterval object, use format() method, for example:

echo $interval->format('%s seconds');

You can see more examples of comparing DateTime objects here. Also check how to use format() method.

Jacek Barecki
  • 429
  • 3
  • 7
0

Yes you can use the built in diff method for the DateTime objects;

$interval = $d1->diff($2);

Heres the manual for it: DateTime::diff

Then to get just the days you can format the interval like this

$interval = $interval->format('%d');
wintheday
  • 139
  • 9
0

You can format the dates to strip off the time like:

if ($d1->format('m/d/Y') == $d2->format('m/d/Y'))
    echo 'equal';
else
    echo 'not equal';

or like this:

if (date_format($d1, 'm/d/Y') == date_format($d2, 'm/d/Y'))
    echo 'equal';
else
    echo 'not equal';

The date_format will put the dates in this format: 04/14/2013

You have to do a date_format on $d2 because it's really '2013-04-14 00:00:00.'

Andrew
  • 2,013
  • 1
  • 23
  • 38