Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/167766-datetime-difference/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/167766-datetime-difference/#findComment-884742
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/167766-datetime-difference/#findComment-884799
Share on other sites

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;
}

Link to comment
https://forums.phpfreaks.com/topic/167766-datetime-difference/#findComment-884803
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.