简体   繁体   English

如何在PHP中获取从开始时间到结束时间的总小时数

[英]How to get the total hour from starting time to end time in php

How can I get the total hour from start time to end time. 我如何获得从开始时间到结束时间的总小时数。

   $start_time = '11:00:00 PM'; // as of 07/08/2013
   $end_time   = '01:00:00 AM'; // as of 08/08/2013

Then output should be: 然后输出应为:

   $total = '02:00:00'; // 12 hour format

You can convert the date strings to time values, then subtract to get the difference in seconds between the two, then simply divide by 3600 to get the difference in hours: 您可以将日期字符串转换为时间值,然后减去以得到两者之间的秒数差,然后简单地除以3600就可以得到小时数差:

$t1 = strtotime('2013-08-07 23:00:00');
$t2 = strtotime('2013-08-08 01:00:00');
$differenceInSeconds = $t2 - $t1;
$differenceInHours = $differenceInSeconds / 3600;

You can check this post from stackoverflow which is similar to your question 您可以从stackoverflow查看此帖子,这与您的问题类似

Calculate total hours between 2 time stamps 计算两个时间戳之间的总小时数

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM