Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compare two DateTime objects in PHP 5.2.8?

Tags:

php

datetime

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

  • DateTime::diff : Get the difference and use that to determine which is more ancient.
  • DateTime::getTimestamp : Get the UNIX timestampt and compare those.

Both these methods are marked in the doco 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 Googled 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 :).

like image 803
RedBlueThing Avatar asked Jun 07 '09 02:06

RedBlueThing


People also ask

Can DateTime objects be compared?

Use the datetime Module to Compare Two Dates The datetime module provides the datetime() method that takes three parameters to create a date from the year, month, and day. After getting dates, they can be compared using the comparison operators.

How can I compare today's date with another date in PHP?

Once you have created your DateTime objects, you can also call the diff() method on one object and pass it the other date in order to calculate the difference between the dates. This will give you back a DateInterval object. $last = new DateTime( "25 Dec 2020" ); $now = new DateTime( "now" );

Can you compare time in PHP?

Interval Between Different Dates In order to compare those two dates we use the method diff() of the first DateTime object with the second DateTime object as argument.


2 Answers

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:~# 
like image 146
Milen A. Radev Avatar answered Sep 26 '22 19:09

Milen A. Radev


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 
like image 35
Roberto Alonso Avatar answered Sep 24 '22 19:09

Roberto Alonso