0

Hi I know you may think that many of them may have asked this question, but the scenario is quite different.

Since I am calculating the total shift hours done by labor monthly wise the hours will be more than 24hrs. I have achieved this by DateInterval.

Now I have 2 times and want to get the difference of those in php.

$workedHours        = '78:25:10';
$miscSpentHours     = '02:26:11';

$totalWorkedHours = $workedHours - $miscSpentHours; // Must Result - 75:59:59

I have used sub, diff, strtotime with it but not getting properly. I am converting it to appropriate date types while conversion.

Any help really appreciated. Thanks.

Touheed Khan
  • 2,149
  • 16
  • 26
Channaveer Hakari
  • 2,769
  • 3
  • 34
  • 45

3 Answers3

2

You can trans it to seconds then do the subtract, then trans the result back.

LF00
  • 27,015
  • 29
  • 156
  • 295
0

I haven't tested this. but this should put you in the right direction

<?php

$hours = 70;//enter number of hours
$zero    = new DateTime('@0');
$offset  = new DateTime('@' . $hours * 3600);//convert to minutes
$diff    = $zero->diff($offset);


$formattedHours = $diff->format('%a Days, %h Hours, %i Minutes');//get format in days hours and minutes

$start_date = new DateTime($formattedHours);//pass in contruct
$since_start = $start_date->diff(new DateTime('02:26:11'));//do difference
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>';

?>
Rotimi
  • 4,783
  • 4
  • 18
  • 27
0
  1. check my functionTimeToSec() to convert HH:MM:SS to seconds
  2. subtract
  3. convert seconds back to HH:MM:SS (check this answer).
Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107