Create a function to add hours onto a deadline
Posted: Thu Jun 26, 2014 5:18 am
Basically I am trying to create a basic case logging system and when somebody opens a new case, the case is assigned a priority with a given number of hours. For example priority 1 is 4 hours, 2 is 9 hours, 3 is 36hours and 4 is 63 hours.
Now adding hours onto a time stamp is easy but the catch is I need to take into account working hours which are 08:30 to 17:30 Monday to Friday. So if a case is given a 4 hour priority and the deadline for this falls after 17:30 on a week day then the deadline is extended to the next working day. Basically the deadline is 4 working hours.
Example:
Case created on: 19/05/2014 16:55 - with Priority 1 (4 Hours) Deadline is now: 20/05/2014 11:55
Make sense?
Another catch is I need to take into account hours On Hold and these two have to be only within working hours. But ill worry about that later.
I am trying to use the modern DateTime class for this as far as possible.
Here is what I have so far, I am struggling with the logic. It works if the deadline is within a day but if I add something like 63 hours is completely fails.
Where am I going wrong here? Brain is fried with this
Now adding hours onto a time stamp is easy but the catch is I need to take into account working hours which are 08:30 to 17:30 Monday to Friday. So if a case is given a 4 hour priority and the deadline for this falls after 17:30 on a week day then the deadline is extended to the next working day. Basically the deadline is 4 working hours.
Example:
Case created on: 19/05/2014 16:55 - with Priority 1 (4 Hours) Deadline is now: 20/05/2014 11:55
Make sense?
Another catch is I need to take into account hours On Hold and these two have to be only within working hours. But ill worry about that later.
I am trying to use the modern DateTime class for this as far as possible.
Here is what I have so far, I am struggling with the logic. It works if the deadline is within a day but if I add something like 63 hours is completely fails.
Where am I going wrong here? Brain is fried with this
Code: Select all
$deadline_c = $this->add_deadline(64);
function add_deadline($hours){
$UTC = new DateTimeZone("UTC");
$now = new DateTime(); //current date/time
$today = new DateTime($now->format('Y-m-d'));//get today's date only
$deadline = $now->add(new DateInterval("PT{$hours}H"));//add on hours from database
//set working hours
$workingday = new DateTime();
$working_daystart = $workingday->setTime(8, 30);
$working_dayend = $workingday->setTime(17, 30);
//check if deadline is after 17:30
if($deadline > $working_dayend){
$interval = $deadline->diff($working_dayend);
//time over the working day in hours and minutes
$over = $interval->format("%H:%I");
//split time
$over = explode(':', $over);
//set to next working since deadline is after 17;30
$nextday = $today->modify('+ 1 day');
$nextday->setTime(8, 30);
//Set deadline to the next day and add on hours and mins
$deadline = $nextday->add(new DateInterval("PT".$over[0]."H"));
$deadline = $deadline->add(new DateInterval("PT".$over[1]."M"));
}
//get days to add on if the next day is a weekend day
$ifweekend = $this->check_day($deadline);
$deadline = $deadline->modify($ifweekend);
$deadline->setTimezone($UTC);//adjust to UTC
$new_time = $deadline->format('Y-m-d H:i');
return $new_time;
}
//checks if passed in date falls on a weekend day and if so passes back day difference
function check_day($deadline) {
$day = $deadline->format(l);
if($day == 'Saturday'){
return '+ 2 days';
}
elseif($deadline == 'Sunday'){
return "+ 1 days";
}
else {
return '+ 0 days';
}
}