datetime difference

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shires82
Forum Newbie
Posts: 5
Joined: Tue Jul 28, 2009 7:17 am

datetime difference

Post 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?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: datetime difference

Post by Mark Baker »

Does it need to take weekends into account as well?
shires82
Forum Newbie
Posts: 5
Joined: Tue Jul 28, 2009 7:17 am

Re: datetime difference

Post by shires82 »

sorry no mon-fri only
thanks
caltoche
Forum Newbie
Posts: 6
Joined: Mon Jul 27, 2009 8:46 am

Re: datetime difference

Post 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
shires82
Forum Newbie
Posts: 5
Joined: Tue Jul 28, 2009 7:17 am

Re: datetime difference

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: datetime difference

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: datetime difference

Post 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';
 
shires82
Forum Newbie
Posts: 5
Joined: Tue Jul 28, 2009 7:17 am

Re: datetime difference

Post 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
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: datetime difference

Post 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';
 
shires82
Forum Newbie
Posts: 5
Joined: Tue Jul 28, 2009 7:17 am

Re: datetime difference

Post by shires82 »

amazing, thanks for your time
Post Reply