JonnoTheDev Posted July 28, 2009 Share Posted July 28, 2009 Hi all. Bit braindead at the moment so need a bit of help. I need a function that will calculate the difference in days/hours/minutes from 2 given datetimes. This is simple enough in itself, not a problem however it must only count the time through a working day of 8 hours i.e. 9-5 so if the start datetime was 2009-07-27 11:00 and the end was 2009-07-28 11:00 then it would return 8 hours not 24. The start and end datetime can be a time prior to 9 and after 5 also so if prior then the counter starts from 9 the same as if after 5pm (start 9am the next day). They could also be on the same day. Any ideas on a formula? Quote Link to comment https://forums.phpfreaks.com/topic/167766-datetime-difference/ Share on other sites More sharing options...
gevans Posted July 28, 2009 Share Posted July 28, 2009 I had a quick play, it shouldn't take too long. I found the best way was to work with the numbers directly not the dates. So split up both date_time strings into 5 number, year, month, day, hours, minutes. Then work from the lowest to the highest. Check the difference in minutes. If the second time's minutes is lower than the first add 60 and take one away from the seconds hours. Check that hours are between 9 and 5. If below 9 round up to 9 if above 5 (17) round down to 17 on the first time. On the second always round down to 17 if out of limits. Then start doing comparison for days/months. Yu may need to adjust by a day if there was a major change in the hours comparisson. Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/167766-datetime-difference/#findComment-884742 Share on other sites More sharing options...
JonnoTheDev Posted July 28, 2009 Author Share Posted July 28, 2009 Thanks Can you lay down a bit of code. Psuedo code if easier. Ta. Quote Link to comment https://forums.phpfreaks.com/topic/167766-datetime-difference/#findComment-884784 Share on other sites More sharing options...
gevans Posted July 28, 2009 Share Posted July 28, 2009 I started having a play. Lots missing, but see if it makes some sense. <?php function cast_int($n) { return (int) $n; } $date_time1 = '2009-07-27 11:05'; $date_time2 = '2009-07-28 3:46'; $date_time_array1 = explode(" ", $date_time1); $date_time_array2 = explode(" ", $date_time2); $time_array1 = explode(":", $date_time_array1[1]); $time_array2 = explode(":", $date_time_array2[1]); $time_array1 = array_map("cast_int", $time_array1); $time_array2 = array_map("cast_int", $time_array2); if($time_array1[1] > $time_array2[1]) { $time_array2[1] += 60; $minute_difference = $time_array2[1]-$time_array1[1]; $time_array2[0]--; } else { $minute_difference = $time_array2[1]-$time_array1[1]; } if($time_array1[0] < 9) { $time_array1[0] = 9; } if($time_array2[0] > 17) { $time_array2[0] = 17; } if($time_array1[0] > $time_array2[0]) { $time_array2[0] += 24; $hour_difference = $time_array2[0]-$time_array1[0]; } else { $hour_difference = $time_array2[0]-$time_array1[0]; } var_dump($date_time_array1); echo '<br />'; var_dump($time_array1); echo '<br />'; var_dump($time_array2); echo '<br />'; var_dump($minute_difference); echo '<br />'; var_dump($hour_difference); echo '<br />'; This needs a lot of work even on the code that's already written. Quote Link to comment https://forums.phpfreaks.com/topic/167766-datetime-difference/#findComment-884799 Share on other sites More sharing options...
patrickmvi Posted July 28, 2009 Share Posted July 28, 2009 This is how I would approach it: function get_working_hours($start_date, $end_date) { $start_date_ts = strtotime($start_date); $end_date_ts = strtotime($end_date); $total_working_hours = 0; for($ts = $start_date_ts; $ts < $end_date_ts; $ts += 3600) { $hour = date("H", $ts); if($hour >= 9 && $hour < 17) { $total_working_hours++; } } return $total_working_hours; } Quote Link to comment https://forums.phpfreaks.com/topic/167766-datetime-difference/#findComment-884803 Share on other sites More sharing options...
gevans Posted July 28, 2009 Share Posted July 28, 2009 That's nice, didn't think of that. Hvaen't tried it but looks like it'd work very nicely. (though it doesn't get the minutes) Quote Link to comment https://forums.phpfreaks.com/topic/167766-datetime-difference/#findComment-884808 Share on other sites More sharing options...
JonnoTheDev Posted July 28, 2009 Author Share Posted July 28, 2009 Sorry, forgot to mention that working days are mon-fri. Weekends do not count. My bad. Quote Link to comment https://forums.phpfreaks.com/topic/167766-datetime-difference/#findComment-884851 Share on other sites More sharing options...
JonnoTheDev Posted July 28, 2009 Author Share Posted July 28, 2009 This is how I would approach it: Yes, needs to return days, hours, minutes Quote Link to comment https://forums.phpfreaks.com/topic/167766-datetime-difference/#findComment-884853 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.