Duration of time spent within a time period

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
peepsnet
Forum Newbie
Posts: 2
Joined: Tue Oct 29, 2013 3:45 am

Duration of time spent within a time period

Post 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
:banghead:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Duration of time spent within a time period

Post 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.
peepsnet
Forum Newbie
Posts: 2
Joined: Tue Oct 29, 2013 3:45 am

Re: Duration of time spent within a time period

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Duration of time spent within a time period

Post 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.
Post Reply