PHP Date Calculation – Algorithm for Date Calculation

date formatmathPHP

I'm working on a project to schedule a machine shop, basically I've got everything covered BUT date calculations, I've got a method called schedule (working on PHP here):

public function schedule($start_date, $duration_in_minutes)

Now my problem is, currently I'm calculating end time manually because time calculations have the following rules:

  1. During weekdays, work with business hours (7:00 AM to 5:00 PM)
  2. Work on Saturdays from 7:00 AM to 2:00 PM
  3. Ignore holidays (in Colombia we have A LOT of holidays)

I already have a lookup table for holidays, I also have a Java version of this algorithm that I wrote for a previous version of the project, but that one's also manual.

Is there any way to calculate an end time from a start time given duration?, my problem is that I have to consider the above rules, I'm looking for a (maybe?) math based solution, however I currently don't have the mind to devise such a solution myself.

I'll be happy to provide code samples if necessary.

Best Answer

I think this might help you:

It's a C# implementation of everything in your algorithm except for holidays (which you should be able to figure out given the rest of the code).

You start off by modeling a working week (Monday-Friday, 8am-5pm, for example, with an hour a day for lunch). Then you can ask it questions like "if I have a task that takes 15 hours and I start at 9:22 on Tuesday, when will I finish?"

If you've done some Java before hopefully you'll be able to read the C#. Alternatively, there's a JavaScript port here:

Related Topic