Date / Time Manipulation

Hi Chaps,
I have some basic code that takes a UNIX time, adds a pre-calulated duration, to give a ‘due date/time’.
I then convert the UNIX time to [Y-m-d H:i:s] format for presentation.
The problem I’m having at the moment is that the due date/time can be something like 2010-05-12 23:55:10. This time is outside of normal working hours, I need to insure the due date/time falls between 09:00 - 17:30 (Mon-Fri).
So if the start time is: 16:00 and the duration is: 5 hours, firstly I need the time to ‘carry over’ to the next day, starting from 09:00, and secondly, if the next day is a weekend, carry over to the next working day. . . . .
Can anyone point me in the right direction?

Hi Dude,

Cheers for that, I can see what your doing, but it’s a bit out of my realm.

Would it be possible to give a bit of code so I can get my brain around it?


I’m litterally walking out the door or i’d code what i’m describing…

Calculate Hours-remaining from Start Time to End of Day1; Subtract from Hours.
While Hours > 0 {
Day ++ % 5 (Monday = 0, Friday = 4)
if (Hours > 8.5) {
Subtract 8.5 from Hours; Continue.
} else {
Endtime = 9:00 + Hours;

If you have PHP 5.3 available, then should be very helpful for you. Combine that with the DateTime object, and you should be in great shape.

Hi, thanks for the link.

Can I make sure I’ve got this right?

I would need to specify valid start-end times for a day (08:30-17:30)
Then add an if construct, that if the ‘due date’ time was outside these times, then apply a DateInterval contruct to move the date to the next day, starting from the valid start time.
Then another if construct, if the ‘due date’ date was on a weekend, apply a second DateInterval contruct to move the date to the next working day.

Does that sound right or have I got it horribly wrong?


Yeah, more or less. The DateTime object is pretty robust, so you could figure out a great deal of things to do with a little bit of object. I don’t think the links I posted are going to magically solve your problem with no additional coding, but in my experience they make the tasks of date manipulation (trivial with Unix time) and date ranges (trivial with more conventional time formats) equally easy between formats.