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:
- During weekdays, work with business hours (7:00 AM to 5:00 PM)
- Work on Saturdays from 7:00 AM to 2:00 PM
- 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.
5
I think this might help you:
- https://bitbucket.org/ant512/workingweek/wiki/Home
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:
- https://bitbucket.org/ant512/workingweekjs/wiki/Home
1
Sounds like a table to list holidays, and check the date against that table, then check the day of week for that date ($dw = date( “w”, $timestamp);).. Have a table for the hours for each day of the week (this makes this configurable). Then subtract out hours from the start date until you have no more hours to subtract out..
pseudocode:
while $durationhours > 0
{
$hoursinday = $hours["dayoftheweek"];
if( $hoursinday > $durationhours )
{
$endtime = $durationhours+$day["starttime"];
}
else
{
$durationhours = $durationhours = $hoursinday;
}
}
Does that work?
2
What you can do is as below:
Keep the timezone of Columbia as default when you are using this function.
- take an array of dates which having holiday and skip those dates all hours in using this.
- check whether it is Saturday, than keep only limited hours you want.
- for all other days keep working hours in calculations.
a) for weekdays/Saturday you will have to check for timings like 7am to 5pm and 7am to 2pm and with conditions and looping of each minutes increment and also condition to check about duration. That’s it.
Its bit told simply but without code, this is enough.