在php 5.3上有这个工作
$datetime1 = new DateTime("2011-10-10 10:00:00");
$datetime2 = new DateTime("2011-10-10 10:45:00");
$interval = $datetime1->diff($datetime2);
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
echo 'Diff. in minutes is: '.($hours * 60 + $minutes);
如何让它在php 5.2上运行?有没有可用的等效功能?
搞定了
$date1 = "2011-10-10 10:00:00";
$date2 = "2011-10-10 10:11:00";
echo round((strtotime($date2) - strtotime($date1)) /60);
答案 0 :(得分:42)
您可以使用strtotime
和date
代替DateTime
:
$datetime1 = strtotime("2011-10-10 10:00:00");
$datetime2 = strtotime("2011-10-10 10:45:00");
$interval = abs($datetime2 - $datetime1);
$minutes = round($interval / 60);
echo 'Diff. in minutes is: '.$minutes;
答案 1 :(得分:0)
如果你需要几天的分钟数,你可以将这一分钟添加到混音中:
$days = $interval->format("%d");
if ($days > 0) {
return ($hours * 60 + $minutes) + ($days * 24 * 60);
}
答案 2 :(得分:0)
试试这个
function time_Diff_Minutes($startTime, $endTime) {
$to_time = strtotime($endTime);
$from_time = strtotime($startTime);
$minutes = ($to_time - $from_time) / 60;
return ($minutes < 0 ? 0 : abs($minutes));
}
echo time_Diff_Minutes("2008-12-13 20:00:00","2008-12-14 08:00:00"); //output 720
echo time_Diff_Minutes("2008-12-14 20:00:00","2008-12-13 08:00:00"); //output 0 (startTime > endTime) Ternary will return 0