PHP 5.2的DateTime :: diff()可以使用什么? [英] What can use for DateTime::diff() for PHP 5.2?
问题描述
PHP 5.2中是否有等效于DateTime :: diff()的函数?
Is there any function equivalent to DateTime::diff() in PHP 5.2?
我的本地服务器是PHP 5.3,并使用DateTime :: diff().然后我发现我的实时站点使用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
PHP代码:
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;
推荐答案
Spudley的答案对我不起作用-从另一个系统中减去任何DateTime会给我的系统带来0.
Spudley's answer doesn't work for me--subtracting any DateTime from another gives 0 on my system.
通过使用带有'U'符的DateTime :: format(自Unix时代以来的秒数),我可以使它工作:
I was able to get it to work by using DateTime::format with the 'U' specifier (seconds since Unix epoch):
$start = new DateTime('2010-10-12');
$end = new DateTime();
$days = round(($end->format('U') - $start->format('U')) / (60*60*24));
这同时适用于我的开发系统(5.3.4)和部署系统(5.2.11).
This works on both my dev system (5.3.4) and my deployment system (5.2.11).
这篇关于PHP 5.2的DateTime :: diff()可以使用什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!