Finding a date by adding work hours to a date.

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 6
Joined: Sun May 19, 2019 3:49 am

Thu Jan 16, 2020 9:38 pm


I could use some help. I'm trying to write a function to find a second datetime, X hours after a first datetime. So if you provide a starting datetime of for example 2020-1-10 09:00:00, and you have 50 hours to add to this, what is the resulting datetime. But there's a bit more to this. Its working hours for each day, not 24 hours for each day. The code has a array like this...

Day Start Hours
Mon 07:00 8
Tue 07:00 8
Wed 07:00 8
Fri 07:00 8
Sat 07:00 4
Sun 00:00 0

So for each day, we have a limited number of hours available. If the start datetime was 9:00 Fri, and you needed to add 15 working hours, the result should be 12:00 Monday ( 6 hours Fri, 4 Sat, then the remaining 5 on Monday )

Just trying to come up with a sensible approach to this, any idea's?

php-forum GURU
php-forum GURU
Posts: 653
Joined: Fri Apr 01, 2016 2:18 am

Thu Jan 16, 2020 10:56 pm

I would say, you take your start date, fast forward one week for every occurancy of combined sheduled hours within your working hours (50 hours = 1.xx weeks), take the rest with modulus, loop over your sheduled hours, forward one day if sheduled hours are fully covered for that day and subtract them, or set the start hour on the starttime and add the remaining hours.
Post Reply