Calculate the time difference for recording at night

I calculate the time difference of the night shift schedule only using time

lets say that I have this data:

$actual_in_time = 6:45 PM //date July 30, 2013 $actual_out_timeout = 7:00 AM //date July 31, 2013 

I need to calculate the time difference in which time in should be converted all the time, therefore

  $actual_in_time = //(some code to convert 6:45 PM to 7:00 PM) $converted_in_time = $actual_in_time; 

Now here is my code:

  $actual_out_time += 86400; $getInterval = $actual_out_time - $converted_in_time; $hours = round($getInterval/60/60, 2, PHP_ROUND_HALF_DOWN); $hours = floor($hours); 

I do not get the desired results. How do you calculate the time difference when only time is the basis?

+4
source share
2 answers

You can use the second parameter in strtotime to get the relative time.

 $start = strtotime($startTime); $end = strtotime($endTime, $start); echo ($end-$start)/3600; 
0
source

Using a DateTime Object

 $start = new DateTime('2000-01-01 6:45 PM'); $end = new DateTime('2000-01-01 7:00 AM'); if ($end<$start)$end->add(new DateInterval('P1D')); $diff = date_diff($start,$end); echo $diff->format('%h hours %i minutes'); 

Add 1 day if your final time is less than the start time.

0
source

Source: https://habr.com/ru/post/1494445/


All Articles