Jump to content

PHP Time Manipulation


kool_samule

Recommended Posts

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!

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.