简体   繁体   中英

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:

$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

Calculate total hours between 2 time stamps

Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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