How do I compare two DateTime objects in PHP 5.2.8?

PhpDatetime

Php Problem Overview


Having a look on the PHP documentation, the following two methods of the DateTime object would both seem to solve my problem:

Both these methods are marked in the http://au.php.net/datetime">doco</a> as being available in version >= 5.3 (and, not surprisingly, if I try to call them I find they don't exist). I can't find any specific documentation for 5.2.8 so I am not sure if there are equivalent methods in my version. I have http://www.google.com.au/search?q=compare+DateTime+PHP&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a">Googled</a> the problem and found an eclectic range of solutions, none of which answer my very simple requirements:

  • How do I compare two DateTime objects?
  • Where can I find the doco for previous PHP versions? Specifically version 5.2.8?

For some context, I have the following code:

$st_dt = new DateTime(verifyParam ('start_date'));
$end_dt = new DateTime(verifyParam ('end_date'));

// is the end date more ancient than the start date?
if ($end_dt < $start_dt) 

Apparently there is no comparison operator on this guy.

Edit

Apparently my assumptions were completely false (thanks Milen for illustrating this so effectively). There is a comparison operator and it works just fine thanks. Sometimes I really miss a compiler. The bug is in the code above, I am sure you will find it much faster than I did :).

Php Solutions


Solution 1 - Php

The following seems to confirm that there are comparison operators for the DateTime class:

dev:~# php
<?php
date_default_timezone_set('Europe/London');

$d1 = new DateTime('2008-08-03 14:52:10');
$d2 = new DateTime('2008-01-03 11:11:10');
var_dump($d1 == $d2);
var_dump($d1 > $d2);
var_dump($d1 < $d2);
?>
bool(false)
bool(true)
bool(false)
dev:~# php -v
PHP 5.2.6-1+lenny3 with Suhosin-Patch 0.9.6.2 (cli) (built: Apr 26 2009 20:09:03)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
dev:~#

Solution 2 - Php

From the official documentation:

> As of PHP 5.2.2, DateTime objects can be compared using comparison operators.

$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");

var_dump($date1 == $date2); // false
var_dump($date1 < $date2); // true
var_dump($date1 > $date2); // false

For PHP versions before 5.2.2 (actually for any version), you can use diff.

$datetime1 = new DateTime('2009-10-11'); // 11 October 2013
$datetime2 = new DateTime('2009-10-13'); // 13 October 2013

$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days'); // +2 days

Solution 3 - Php

You can also compare epoch seconds :

$d1->format('U') < $d2->format('U')

Source : http://laughingmeme.org/2007/02/27/looking-at-php5s-datetime-and-datetimezone/ (quite interesting article about DateTime)

Solution 4 - Php

If you want to compare dates and not time, you could use this:

$d1->format("Y-m-d") == $d2->format("Y-m-d")

Solution 5 - Php

$elapsed = '2592000';
// Time in the past
$time_past = '2014-07-16 11:35:33';
$time_past = strtotime($time_past);
 
// Add a month to that time
$time_past = $time_past + $elapsed;
 
// Time NOW
$time_now = time();
 
// Check if its been a month since time past
if($time_past > $time_now){
    echo 'Hasnt been a month';    
}else{
    echo 'Been longer than a month';
}

Solution 6 - Php

As of PHP 7.x, you can use the following:

$aDate = new \DateTime('@'.(time()));
$bDate = new \DateTime('@'.(time() - 3600));

$aDate <=> $bDate; // => 1, `$aDate` is newer than `$bDate`

Solution 7 - Php

You mentioned DateTime::diff to compare two dates, you can use it to get a more precise value than just a boolean.

For example the number of days:

$date1 = new DateTime('2021-07-09');
$date2 = new DateTime('2020-10-21');

echo $date1->diff($date2)->format('%R%a days');

But you could also use an alias of DateTime::diff which is date_diff (PHP 5 >= 5.3.0, PHP 7, PHP 8)

$date1 = new DateTime('2021-07-09');
$date2 = new DateTime('2020-10-21');

echo date_diff($date1, $date2)->format('%R%a days');

Solution 8 - Php

This may help you.

$today = date("m-d-Y H:i:s");
$thisMonth =date("m");
$thisYear = date("y");
$expectedDate = ($thisMonth+1)."-08-$thisYear 23:58:00";

    		
if (strtotime($expectedDate) > strtotime($today)) {
	echo "Expected date is greater then current date";
	return ;
} else
{
 echo "Expected date is lesser then current date";
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRedBlueThingView Question on Stackoverflow
Solution 1 - PhpMilen A. RadevView Answer on Stackoverflow
Solution 2 - PhpRoberto AlonsoView Answer on Stackoverflow
Solution 3 - PhpJulienView Answer on Stackoverflow
Solution 4 - PhpblablablaView Answer on Stackoverflow
Solution 5 - PhpKyle CootsView Answer on Stackoverflow
Solution 6 - Phpjens1oView Answer on Stackoverflow
Solution 7 - PhpDylan KasView Answer on Stackoverflow
Solution 8 - PhpTarun GuptaView Answer on Stackoverflow