PHP Time Manipulation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

PHP Time Manipulation

Post 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!
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: PHP Time Manipulation

Post 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
?>
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

Re: PHP Time Manipulation

Post 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!
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

Re: PHP Time Manipulation

Post 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
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: PHP Time Manipulation

Post by internet-solution »

You people get weekends off ?!?!? :roll:

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 ...
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

Re: PHP Time Manipulation

Post 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!
Post Reply