php 如何计算两个日期时间之间的小时,分钟?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21692911/
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
How to calculate the hour,min between two date time?
提问by alamin8031
I have two date's
我有两个约会对象
$date1 = "2014-02-11 04:04:26 AM"
$date2 = "2014-02-11 05:36:56 AM"
I want to calculate the difference and display it as follows
我想计算差异并显示如下
1 hour 32 minutes
1 hour 32 minutes
回答by Shankar Damodaran
Make use of DateTime::diffof the DateTimeClass
使用DateTime::diff的的DateTime等级
<?php
$datetime1 = new DateTime('2014-02-11 04:04:26 AM');
$datetime2 = new DateTime('2014-02-11 05:36:56 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%h')." Hours ".$interval->format('%i')." Minutes";
OUTPUT :
OUTPUT :
1 Hours 32 Minutes
回答by Vedant Joshi
Simply convert both dates to timestamp if dont want to do it in complex way... Something like this
如果不想以复杂的方式将两个日期都转换为时间戳...像这样
$dateDiff = intval((strtotime($date1)-strtotime($date2))/60);
$hours = intval($dateDiff/60);
$minutes = $dateDiff%60;
and there you go...
然后你去...
Thank you...
谢谢...

