php 两个日期之间的时间差(以分钟为单位)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7929612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:38:49 来源:igfitidea点击:
Time diff in minutes between 2 dates
提问by sam
Got this working on php 5.3
让这个在 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);
How can i make it work on php 5.2 ? are there any equivalent functions available??
我怎样才能让它在 php 5.2 上工作?有没有等效的功能??
Got it working
搞定了
$date1 = "2011-10-10 10:00:00";
$date2 = "2011-10-10 10:11:00";
echo round((strtotime($date2) - strtotime($date1)) /60);
回答by hsz
回答by raphaelstolt
If you need the minutes spanning several days you could add this one into the mix:
如果您需要跨越几天的分钟数,您可以将这个添加到组合中:
$days = $interval->format("%d");
if ($days > 0) {
return ($hours * 60 + $minutes) + ($days * 24 * 60);
}
回答by HIR
Try this
尝试这个
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