How to Compare Two Datetime Objects in PHP 5.2.8

How do I compare two DateTime objects in PHP 5.2.8?

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:~#

PHP: Comparing two DateTime objects with different timezones

According to the manual:

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

You've chosen a very confusing example that includes a fixed date that's not clear if it's supposed to be in the past and a time zone that's currently not active (it's CEST here in Western Europe until the end of October). Whatever, I see nothing wrong in your output: 2015-10-09 10:53:42 CET (which equals 2015-10-09 09:53:42 UTC) is clearly greater than 2015-10-09 10:28:01 Europe/Paris (which equals 2015-10-09 08:28:01 UTC).

With a slightly better example we can guess the operands do work as expected:

    echo "Same date:\n";
$a = new DateTime('2015-01-31 01:23:45 UTC');
$b = new DateTime('2015-01-31 02:23:45 Europe/Paris');
var_dump($a, $b, $a<$b, $a==$b, $a>$b);
echo "\n";

echo "First greater than second:\n";
$a = new DateTime('2015-01-31 01:23:46 UTC');
$b = new DateTime('2015-01-31 02:23:45 Europe/Paris');
var_dump($a, $b, $a<$b, $a==$b, $a>$b);
echo "\n";

echo "First less than second:\n";
$a = new DateTime('2015-01-31 01:23:45 UTC');
$b = new DateTime('2015-01-31 02:23:46 Europe/Paris');
var_dump($a, $b, $a<$b, $a==$b, $a>$b);
Same date:
[...]
bool(false)
bool(true)
bool(false)

First greater than second:
[...]
bool(false)
bool(false)
bool(true)

First less than second:
[...]
bool(true)
bool(false)
bool(false)

Demo with full code.

Another example:

$date_2014 = new DateTime('2014-12-31 23:00:00 -07:00');
$date_2015 = new DateTime('2015-01-01 05:00:00 Asia/Tokyo');
var_dump($date_2014<$date_2015); // bool(false)

PHP - comparing datetime objects with different timezone

DateTime objects with different time zones are considered the same for simple comparison if they represent the same time.

Example:

$dt1 = new DateTime('2019-10-10 00:00', new DateTimeZone('UTC'));
$dt2 = new DateTime('2019-10-10 03:00', new DateTimeZone('Europe/Bucharest'));
var_dump($dt1 == $dt2); //bool(true)

The result of comparison is equal because at the time of 2019-10-10 00:00 it was already 03:00 in Bucharest.
The comparisons with larger and smaller work analogously.

Note: DateTime has implemented special comparisons for this and reacts differently than the comparison of "normal objects"

How to Compare Two Date with Time in Php

 $date1 = "2018-02-06 15:09:44";
$date2 = "2018-02-06 16:09:44";

if(strtotime($date1) < strtotime($date2)) {
echo "date1 less than date2";
} else {
echo "date1 is greater than date2;
}

Comparing datetime objects. The right way

The >, < and == operators can be reliably used with DateTime objects.



Related Topics



Leave a reply



Submit