Page 1 of 1

datetime difference

Posted: Tue Jul 28, 2009 7:19 am
by shires82
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?

Re: datetime difference

Posted: Tue Jul 28, 2009 7:53 am
by Mark Baker
Does it need to take weekends into account as well?

Re: datetime difference

Posted: Tue Jul 28, 2009 7:59 am
by shires82
sorry no mon-fri only
thanks

Re: datetime difference

Posted: Tue Jul 28, 2009 9:59 am
by caltoche
You can convert the date to a unix timestamp (mktime function), calculate the difference (you get an integer), an then, transform the integer into a date with the time() function.

You can also use this great software for managing your time at work : http://www.timeedition.com/en/index.html

Re: datetime difference

Posted: Tue Jul 28, 2009 10:03 am
by shires82
caltoche wrote:You can convert the date to a unix timestamp (mktime function), calculate the difference (you get an integer), an then, transform the integer into a date with the time() function.

You can also use this great software for managing your time at work : http://www.timeedition.com/en/index.html
Have you read my requirement?

Re: datetime difference

Posted: Tue Jul 28, 2009 10:04 am
by pickle
Convert the dates to timestamps. You can then determine how many weeks they are apart by using date(). Multiply the number of weeks by 5 and you've got your work days. Multiply that by 8 and you've got your work hours. Of course, you'll need to do some extra logic to handle partial weeks and partial days.

Re: datetime difference

Posted: Tue Jul 28, 2009 10:53 am
by Mark Baker

Code: Select all

 
$shiftStartTime = '09:00';
$shiftEndTime = '17:00';
 
$startDate = '2009-07-23 15:00';
$endDate = '2009-07-24 12:00';
 
function calculateHours($startDate,$endDate,$shiftStartTime,$shiftEndTime) {
    $shiftStart = strtotime(date('d-M-Y ').$shiftStartTime);
    $shiftEnd = strtotime(date('d-M-Y ').$shiftEndTime);
 
    $loopStartDate = strtotime($startDate);
    $loopEndDate = strtotime($endDate);
 
    $dayLength = ($shiftEnd - $shiftStart) / 3600;
 
    if ((date('G',$loopStartDate) < date('G',$shiftStart)) ||
        (date('G',$loopStartDate) > date('G',$shiftEnd))) {
        echo 'Start date outside of shift hours<br />';
        return false;
    }
 
    if ((date('H',$loopEndDate) < date('H',$shiftStart)) ||
        (date('H',$loopStartDate) > date('H',$shiftEnd))) {
        echo 'Start date outside of shift hours<br />';
        return false;
    }
 
    if (date('d-M-Y',$loopStartDate) == date('d-M-Y',$loopEndDate)) {
        return ($loopEndDate - $loopStartDate) / 3600;
    } else {
        $endOfStartDay = strtotime(date('d-M-Y ',$loopStartDate).$shiftEndTime);
        $startOfEndDay = strtotime(date('d-M-Y ',$loopEndDate).$shiftStartTime);
 
        $startDateHours = ($endOfStartDay - $loopStartDate) / 3600;
        $endDateHours = ($loopEndDate - $startOfEndDay) / 3600;
 
        $loopStartDate += 86400;
        $loopEndDate -= 86400;
 
        $days = 0;
        for ($i = $loopStartDate; $i <= $loopEndDate; $i += 86400) {
            if (date('N',$i) < 6) {
                echo date('D, d-M-Y',$i).'<br />';
                $days++;
            }
        }
        return ($days * $dayLength) + $startDateHours + $endDateHours;
    }
}
 
 
echo 'Calculated Time is '.calculateHours($startDate,$endDate,$shiftStartTime,$shiftEndTime).' hours';
 

Re: datetime difference

Posted: Tue Jul 28, 2009 11:17 am
by shires82
Not bad Mark however needs to return minutes also i.e. not 19.5 hours - 19 hours 30 minutes.
Also not return an error if the start / end time is out of hours. It will need to progress the time i.e if the start date was mon 12th at 17:55 then it progresses to tues 13th 9:00 same as if time is prior to 9 i.e 07:30 would start at 9:00 on the same day. Same applies if your started / ended on a weekend. The start date would then start from monday at 9:00

Re: datetime difference

Posted: Tue Jul 28, 2009 11:58 am
by Mark Baker

Code: Select all

 
$shiftStartTime = '09:00';
$shiftEndTime = '17:00';
 
$startDate = '2009-07-25 9:00';
$endDate = '2009-07-27 9:15';
 
function calculateHours($startDate,$endDate,$shiftStartTime,$shiftEndTime) {
    $shiftStart = strtotime(date('d-M-Y ').$shiftStartTime);
    $shiftEnd = strtotime(date('d-M-Y ').$shiftEndTime);
 
    $loopStartDate = strtotime($startDate);
    $loopEndDate = strtotime($endDate);
 
    $dayLength = ($shiftEnd - $shiftStart) / 3600;
 
    if ((date('G',$loopStartDate) < date('G',$shiftStart)) ||
        (date('G',$loopStartDate) > date('G',$shiftEnd))) {
        echo 'Start date outside of shift hours<br />';
        $loopStartDate = strtotime(date('d-M-Y ',$loopStartDate).$shiftStartTime) + 86400;
    }
    while (date('N',$loopStartDate) >= 6) {
        $loopStartDate +=  + 86400;
    }
 
    if ((date('H',$loopEndDate) < date('H',$shiftStart)) ||
        (date('H',$loopEndDate) > date('H',$shiftEnd))) {
        echo 'End date outside of shift hours<br />';
        $loopEndDate = strtotime(date('d-M-Y ',$loopEndDate).$shiftStartTime) + 86400;
    }
    while (date('N',$loopEndDate) >= 6) {
        $loopEndDate +=  + 86400;
    }
 
    if (date('d-M-Y',$loopStartDate) == date('d-M-Y',$loopEndDate)) {
        $time = ($loopEndDate - $loopStartDate) / 60;
        return floor($time / 60).':'.fmod($time,60);
    } else {
        $endOfStartDay = strtotime(date('d-M-Y ',$loopStartDate).$shiftEndTime);
        $startOfEndDay = strtotime(date('d-M-Y ',$loopEndDate).$shiftStartTime);
 
        $startDateHours = ($endOfStartDay - $loopStartDate) / 3600;
        $endDateHours = ($loopEndDate - $startOfEndDay) / 3600;
 
        $loopStartDate += 86400;
        $loopEndDate -= 86400;
 
        $days = 0;
        for ($i = $loopStartDate; $i <= $loopEndDate; $i += 86400) {
            if (date('N',$i) < 6) {
                echo date('D, d-M-Y',$i).'<br />';
                $days++;
            }
        }
        $time = ($days * $dayLength) + $startDateHours + $endDateHours;
        return floor($time).':'.(fmod($time,1) * 60);
    }
}
 
 
echo 'Calculated Time is '.calculateHours($startDate,$endDate,$shiftStartTime,$shiftEndTime).' hours';
 

Re: datetime difference

Posted: Wed Jul 29, 2009 10:12 am
by shires82
amazing, thanks for your time