1

I currently have a script to calculate the difference between two dates, in days:

$now      = new DateTime();
$birthday = new DateTime('1973-04-18 09:48:00');
$interval = $now->diff($birthday);
echo $interval->format('%a days'); 

However, I keep on getting this error:

Fatal error: Call to undefined method DateTime::diff() in /home/test/public_html/age.php on line 4

I found out that this code is only supposed to work with PHP version 5.3, however, is there a workaround for 5.2.17?

Kara
  • 6,115
  • 16
  • 50
  • 57
user1981730
  • 89
  • 3
  • 12
  • 4
    You shouldn't use that old php versions – Nox Feb 03 '13 at 17:11
  • See this question. Some of the answers have the code you can use. http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – John Conde Feb 03 '13 at 17:20
  • 1
    There is always the `$days = ($now->getTimestamp() - $birthday->getTimestamp()) / (24*3600)` – Déjà vu Feb 03 '13 at 17:29
  • ring0 getTimestamp() is also only available in PHP 5.3, see http://www.php.net/manual/en/datetime.gettimestamp.php – jenovachild Sep 03 '13 at 00:41
  • possible duplicate of [What can use for DateTime::diff() for PHP 5.2?](http://stackoverflow.com/questions/4033224/what-can-use-for-datetimediff-for-php-5-2) – Glavić Oct 12 '13 at 16:11
  • @Nox there are those of us who have no control over what version of PHP is being used... – Tom Miller Aug 09 '15 at 21:41

0 Answers0