Undefined date_diff()

James picture James · Aug 13, 2010 · Viewed 18k times · Source

I'm trying to use date_diff():

$datetime1 = date_create('19.03.2010');
$datetime2 = date_create('22.04.2010');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%d days');

Its doesn't work for me, gives an error:

Call to undefined function  date_diff()

How can I get it work?

PHP 5.2 is used.

Thanks.

Answer

Peter O'Callaghan picture Peter O'Callaghan · Aug 13, 2010

The function date_diff requires a PHP version of 5.3 or greater.

UPDATE

An example for PHP 5.2 (taken from the date_diff user comments).

<?php 
function date_diff($date1, $date2) { 
    $current = $date1; 
    $datetime2 = date_create($date2); 
    $count = 0; 
    while(date_create($current) < $datetime2){ 
        $current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current))); 
        $count++; 
    } 
    return $count; 
} 

echo (date_diff('2010-3-9', '2011-4-10')." days <br \>"); 
?>