kool_samule Posted May 28, 2010 Share Posted May 28, 2010 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). $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! Quote Link to comment Share on other sites More sharing options...
kool_samule Posted May 28, 2010 Author Share Posted May 28, 2010 I now have this code, that will calculate the correct times, but does anyone know how I can check to see if the correct time falls on a weekend, and if so, move it to the next Monday? $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 $task_due = date('Y-m-d H:i:s',$test_date); Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 28, 2010 Share Posted May 28, 2010 You could use the date function to check if the final date is a weekend and then if so add more time to it. Maybe something along the lines of: $day_of_week = date('w', $date); switch ( $day_of_week ) { case 0: //sunday //add 24 hours to date break; case 6: //saturday //add 48 hours to date break; default: //do nothing break; } Quote Link to comment Share on other sites More sharing options...
kool_samule Posted May 28, 2010 Author Share Posted May 28, 2010 OK, thanks dude, this is what I've got: <?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; ?> It looks good, could you just have a peep at it and let me know if you can see anything wrong with it? Cheers Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 28, 2010 Share Posted May 28, 2010 You'll probably want to use strtotime for adding time to your dates inside the date() function. Quote Link to comment Share on other sites More sharing options...
kool_samule Posted May 28, 2010 Author Share Posted May 28, 2010 Like this? case 0: //sunday $due = strtotime(date("Y-m-d",$test_date+3600*24 )); $task_due = date('Y-m-d H:i:s', $due); break; Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 28, 2010 Share Posted May 28, 2010 No. You'll use strtotime inside the date function and wrap the second parameter in it. date( 'w', strtotime() ) Quote Link to comment Share on other sites More sharing options...
kool_samule Posted May 28, 2010 Author Share Posted May 28, 2010 Uhh....like this? $day_of_week = date('w', strtotime(date("Y-m-d",$test_date)); switch ( $day_of_week ) { case 0: //sunday +3600*24; break; case 6: //saturday +3600*48; break; default: $test_date; break; } Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 28, 2010 Share Posted May 28, 2010 Closer, but again, we don't use date() inside strtotime() in this instance. strtotime() is going to return an integer, the number of seconds since the "epoch of time." That's why we don't use date() inside strtotime(). It's essentially a counter of where time is according to that standard. It can also parse just about any textual representation of a date. $day_of_week is an integer. If it's 0 or 6 we add 24 or 48 hours, in seconds, to your date in question (which is still $test_date). strtotime($test_date) gives you seconds, then add the proper amount of time in seconds and save to new variable, $new_date. And at the end, $new_date holds your time, in seconds. However you want to display your date, you'll use $new_date with the date() function. Example: date('F j, y', $new_date); $day_of_week = date('w', $test_date); switch ($day_of_week) { case 0: //sunday $new_date = strtotime($test_date) + (3600 * 24); break; case 6: //saturday $new_date = strtotime($test_date) + (3600 * 48); break; default: //every other day $new_date = strtotime($test_date); break; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.