Page 1 of 1
PHP Time Manipulation
Posted: Fri May 28, 2010 3:37 am
by koolsamule
Hi Chaps,
I have a MySQL (Y-m-d H:i:s) starting date. . .
I then have a task duration in seconds/minutes
What I'm after is a task 'due' date, which falls between normal working hours (09:00 - 17:30).
Code: Select all
$task_start = 2010-05-27 16:08:23;
$duration = 45000; // Which I guess is 12.5 hours????
$start_str = strtotime($task_start);
$calc = $start_str + $duration;
$due_date = date('Y-m-d H:i:s', $calc);
echo $due_date; // 2010-05-28 04:38:23
As you can see, the calculation is correct, but the time falls outside the normal working hours, so I need some sort of function to check, then re-calculate so it falls between 09:00-17:30.
Any help would be awesome!
Re: PHP Time Manipulation
Posted: Fri May 28, 2010 7:38 am
by internet-solution
Try this, should work for any task duration
Code: Select all
<?php
$task_start = "2010-05-27 16:08:23";
$duration = 45000; // Which I guess is 12.5 hours????
$start_str = strtotime($task_start);
$calc = $start_str + $duration;
$due_date = date('Y-m-d H:i:s',$calc);
$test_date=$calc;
$test_endtime=strtotime(date("Y-m-d",$start_str )." 17:30"); //start date 17:30
$test_starttime=strtotime(date("Y-m-d",$start_str+3600*24)." 9:00"); //next date 9:00
while ($test_date > $test_endtime)
{
$diff=$test_date - $test_endtime;
$test_date=$test_starttime + $diff;
$test_endtime+=3600*24;
$test_starttime+=3600*24;
}
echo "<br>".$due_date; // 2010-05-28 04:38:23
echo "<br>".date('Y-m-d H:i:s',$test_date) //2010-05-29 11:38:23
?>
Re: PHP Time Manipulation
Posted: Fri May 28, 2010 9:36 am
by koolsamule
Spot on dude, many thanks . . .
That logic was doing my brain, would have screwed up my weekend . . .and bank holiday!
Thanks a million!
Re: PHP Time Manipulation
Posted: Fri May 28, 2010 9:51 am
by koolsamule
Uhhh . . .
Think I missed something, the time check works time, but how do I check to see if the next working day is a monday / not a saturday or sunday?
Cheers
Re: PHP Time Manipulation
Posted: Fri May 28, 2010 10:19 am
by internet-solution
You people get weekends off ?!?!?
Some clues - when setting $test_starttime and $test_endtime within the while loop check for week days, if friday set them to Monday 0900 and 1730 respectively.
Let me know if managed to revise the code, or I will spoil my bank holiday weekend to write up the solution. The weather forecast is grim anyway ...
Re: PHP Time Manipulation
Posted: Fri May 28, 2010 10:31 am
by koolsamule
Yo, this is what I have so far . . . it works at first glance, would you mind just checking it over?
Code: Select all
<?php
$task_start = "2010-05-27 16:08:23";
$duration = 45000; // Which I guess is 12.5 hours????
$start_str = strtotime($task_start);
$calc = $start_str + $duration;
$due_date = date('Y-m-d H:i:s',$calc);
$test_date=$calc;
$test_endtime=strtotime(date("Y-m-d",$start_str )." 17:30"); //start date 17:30
$test_starttime=strtotime(date("Y-m-d",$start_str+3600*24)." 9:00"); //next date 9:00
while ($test_date > $test_endtime)
{
$diff=$test_date - $test_endtime;
$test_date=$test_starttime + $diff;
$test_endtime+=3600*24;
$test_starttime+=3600*24;
}
$day_of_week = date('w', $test_date);
switch ( $day_of_week )
{
case 0: //sunday
$task_due = date('Y-m-d H:i:s', $test_date+3600*24);
break;
case 6: //saturday
$task_due = date('Y-m-d H:i:s', $test_date+3600*48);
break;
default:
$task_due = date('Y-m-d H:i:s',$test_date);
break;
}
echo $task_due;
?>
Cheers!