Page 1 of 1
Duration of time spent within a time period
Posted: Tue Oct 29, 2013 3:56 am
by peepsnet
I am unable to figure out how to do this.
I am looking to have the amount of time a worker worked between two times returned in either min or hour/min
if I started working at 1400 for 8 hours, how much time did the worker spend between 1800-0600. I can see it is 4 hours. But I don't know how to calculate this with PHP.
Thanks in advance,
Don

Re: Duration of time spent within a time period
Posted: Tue Oct 29, 2013 4:16 am
by requinix
Both the starting and endings times can be either inside or outside the window. That makes four combinations:
1. The starting time is within the window and the ending time is not ($window_end - $start_time)
2. The starting time is before the window and the ending time is within ($end_time - $window_start)
3. Both starting and ending times are within the window ($end_time - $start_time)
4. Both starting and ending times are outside the window ($window_end - $window_start)
Code: Select all
window_start window_end
| |
1 | start_time-------+---end_time
| |
2 start_time---+---------end_time |
| |
3 |start_time--end_time|
| |
4 start_time---+--------------------+---end_time
| |
If you look carefully you'll see that the logic can be reduced to a simple
Code: Select all
min($window_end, $end_time) - max($window_start, $start_time)
Take care with overflow like as with the 1800-0600 window: it'll be easier to treat that like 1800-3000.
Re: Duration of time spent within a time period
Posted: Tue Oct 29, 2013 4:31 am
by peepsnet
Thank you for the quick response but I am not sure that it's complete.
I am trying to use the numbers as dates. I would not be able to use 3000 if I do that... I believe??
Can you be more specific???
Don
Re: Duration of time spent within a time period
Posted: Tue Oct 29, 2013 2:42 pm
by requinix
You'd just use 3000 for the math, not actually display the time that way.
Otherwise you'd have to do more checks to see exactly which values are, in fact, larger than others. It's roughly the same amount of work either way but it makes more sense to me to use 3000.