tags:

views:

80

answers:

1

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.

+7  A: 

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 \>"); 
?>
Cags
Any way to use it on PHP 5.2?
Happy
Added a variation.
Cags
@Cags: this solution is very inefficient
Happy
You asked how to get date_diff working, I answered with the clear answer of you can't and offered a simple solution, which came directly from the user comments for date_diff, I simply copy pasted it. There are numerous ways to tackle the issue. Subtract the two strtotime values and dividing it by the right segments would be another way. If you want a 'simple' way, upgrade PHP.
Cags
Thanks man, but this is a killing-solution. It will be awesome, if you try yourself before posting.
Happy