[英]Date time difference miss to calculate the days to hour
我有一个日期时差-
我需要将那些天也计算成小时。
$start = '2016-05-26 19:05:00';
$end = '2016-05-29 17:05:00';
$datetime1 = new DateTime($start);
$datetime2 = new DateTime($end);
$interval = $datetime1->diff($datetime2);
echo $interval->format("%h Hours %i Min %s Sec"); //22 Hours 0 Min 0 Sec
我知道这个:
echo $interval->format('%d days');
我尝试这样:
echo $interval->format("(%h + %d * 24) Hours %i Min %s Sec"); //(22 + 8 * 24) Hours 0 Min 0 Sec
$start_date = new DateTime('2007-09-01 04:10:58');
$since_start = $start_date->diff(new DateTime('2012-09-11 10:25:00'));
echo $since_start->days.' days total<br>';
echo $since_start->y.' years<br>';
echo $since_start->m.' months<br>';
echo $since_start->d.' days<br>';
echo $since_start->h.' hours<br>';
echo $since_start->i.' minutes<br>';
echo $since_start->s.' seconds<br>';
echo (($since_start->days)*24)+$since_start->h.' Total Hours<br>';
用这个来计算天数,然后乘以24加上剩余的小时数
它很简单,尽管不能自动为您完成,但您必须编写代码。
这就是您所需要的:
$start = '2016-05-26 19:05:00';
$end = '2016-05-29 17:05:00';
$datetime1 = new DateTime($start);
$datetime2 = new DateTime($end);
$interval = $datetime1->diff($datetime2);
// Below this line is where the magic takes place:
$hourDiff = ($interval->d*24)+($interval->h); //here we calculate the hours
echo $hourDiff //prints 77 because 2 days*24 hours each + 22 hours = 77
在我的示例中,我还考虑了年份和月份(分别为y和m),但是如果不想使用它们,可以将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.