Is there any function equivalent to DateTime::diff() in PHP 5.2?
My local server is PHP 5.3 and using DateTime::diff(). then I found that my live site uses PHP 5.2 and gives an error.
Fatal error: Call to undefined method DateTime::diff() in /var/www/some/other/dir/web/daikon/modules/projects/views/admin/log/admin_log_list.php on line 40
The PHP code:
foreach ($logs as $key => $list){
...
// show date in European way dd-mm-yyyy not in MySQL way yyyy-mm-dd
$newdate =new DateTime($list['date']) ;
echo "<td class=\"left\" width=\"8%\">".$newdate->format('d-m-Y')."</td>\n";
$starttime = new DateTime($list['start_time']);
echo "<td width=\"7%\">".date_format($starttime, 'H:i')."</td>\n";
$finishtime = new DateTime($list['finish_time']);
echo "<td width=\"8%\">".date_format($finishtime, 'H:i')."</td>\n";
$timediff = 0;
$interval = $starttime->diff($finishtime);
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$timediff = $hours * 60 + $minutes;
I just needed that ( unfortunately ) for a WordPress plugin. This I use the function in 2 times:
In my class calling ->diff() ( my class extends DateTime, so $this is the reference DateTime )
Then I recreated a fake DateInterval class ( because DateInterval is only valid in PHP >= 5.3 ) as follows:
Hope that helps somebody.
Please observe that if your DateTime object was created from a date string without any timezone information (as in '2012-01-01 05:00:00' like from mysql), then setting the timezone later with DateTimeZone objects via setTimeZone() does not change the DateTime objects internal timestamp.
Here is the best solution for your question.
Read the contrib notes in the PHP date_diff page http://us3.php.net/manual/en/function.date-diff.php