0

I am trying to create a function that will work out the time difference between 2 times but only includes WORKING HOURS. This is for a project which tracks the time it takes for a job to be completed.

Working hours are 9am - 5pm, Monday - Friday.

  • Example 1
    If the job starts at 4:50pm, and somebody looks at the page the next day at 10am, the timer will say 1h 10m 0s.
  • Example 2
    If the job starts 6:23pm, and somebody looks at the page the next day at 9:30am, the timer will say 30m 0s.
  • Example 3
    If the job starts on Friday at 1:20pm, and somebody looks at the page no Tuesday at 4pm, the timer will say 18h 40m 0s. (It excluded Saturday and Sunday!)
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Clarkey
  • 698
  • 3
  • 11
  • 28
  • check the answers on this page [link](http://stackoverflow.com/questions/2091838/time-difference-between-2-dates-ignoring-weekend-time) – Amine Matmati Feb 19 '13 at 13:47
  • @Peter I have tried to cycling through every hour in a for loop and trying to narrow it down with if statements, but it just gets messy and confusing as I get into the minutes and seconds. I really have a mental block to this at the minute, if you can show me how you would go about it, I'd be very grateful. – Clarkey Feb 19 '13 at 13:54
  • @AmineMatmati I've had a look, this is more focused on skipping weekends rather than everything outside of working hours. – Clarkey Feb 19 '13 at 14:00
  • You already asked this question: http://stackoverflow.com/questions/14937144/php-exclude-all-non-business-hours-days-from-time-difference – Benny Hill Feb 19 '13 at 15:38

1 Answers1

1

this can be a bit overwhelming, but in the link @Amine Matmati provided you have all the tools you need. With it you could easily walk through each day from the starting timestamp up to the present, and skip weekends and off hours.

It's a bit clunky, but it would work.

strtotime(str, timestamp) takes a string that is a command in almost plain English, and if you want, a reference timestamp. This is what you would use to run from your current timestamp all the way to the end timestamp.

ie. strtotime('end of the working day', timestamp of when the job started);

[NB: 'end of the day' is not an acceptable string, check the link to the PHP manual at the bottom for that, I just wanted you to see how it would be useful to you]

So if you built a loop that walked from the original timestamp to the present, you could count the number of working hours as you went.

I'll show you the logic in pseudo code:

$Present is present timestamp;
$Start is the starting timestamp;
$Hours will be the accumulated working hours between starting timestamp and present timestamp;

loop while present timestamp not reached {

if $Start does not occur on the same day as $Present{
  if $Start occurs inside of working hours {
    find the time to the end of the working day;
    add that time to $Hours;
  }

  find next day;
  if the next day is Saturday{
    update $Start to the working start of next Monday;
  }
  else{
    update $Start to the working start of the next day;
  }
}
else{
  find the time between $Start and $Present;
  add that time to $Hours;
  update $Start to $Present or Break;
  }
}

It does assume that the starting time does not occur on weekends, and does not occur outside (ie. before) working hours on the same day as the present timestamp, but controlling for those is very easy.

Things that will be your friend in this are:

@Amine Matmati's link: Time difference between 2 dates ignoring weekend time

PHP date(): http://php.net/manual/en/function.date.php
PHP strtotime(): http://www.php.net/manual/en/function.strtotime.php

Happy coding!

Community
  • 1
  • 1
abase
  • 220
  • 1
  • 8