Jump to content

Begin date and end date, but no weekends!


Jahnoes

Recommended Posts

Hello,

 

I am making a system to plan my work days.

If i begin on a project on 01-10-2007, and the project is 100 hour and i work 10 hours a day, it will cost 10 days for me too work.

 

The answer is simple, project is done @ 10-10-2007, but i dont work in the weekend.

So it must be 12-10-2007, i got the following script;, but it aint working right.

 

Because when the begin date and end date is more then 2 weeks, and more than 2 weekends, it doenst count the weekends good.

Can someone help me, or is there somebody with a better solution? I am working two days on it, but i cant get it right :(

 

 


<?php

$begin_date = strtotime( date( '2007-10-01' ) );

$hours = 100;
$workdays = 10;

$total_days = $hours / $workdays; 
$total_days *= (60 * 60 * 24);

$weekend = $total_days / 7 * 2;

$enddate = $begin_date + $total_days + $weekend;

if( date('D', $enddate ) == 'Sat' ) {
echo 'End date <b>' . date('d-m-Y', $enddate + (2*86400)) . '</b> 2 days<br /><br />';
}

elseif( date('D', $enddate ) == 'Sun' ) {
echo 'End date <b>' . date('d-m-Y', $enddate + (1*86400)) . '</b> 1 day<br /><br />';
}

else
{
echo 'End date <b>' . date('d-m-Y', $enddate) . '</b><br /><br />';
}

?>

Link to comment
Share on other sites

Well, you need to first figure out how many days it will take.

Then use logic to determine how many weekends will be in there, based on the start day and end day. Then for each weekend, add two days to the total days. If the number of weekends is > 5 (I think) you'll have to check again for new weekends generated. I think you'll need to make a recursive function to do this.

Link to comment
Share on other sites

This is what I would do:

 

Find begin date and total days needed to work.

Determine if the start date is a weekend.  If it isn't, $day++.  Else, nothing.

Add one day to the start date (through a timestamp or some other method).  Check if it is a weekend.  If it isn't, $workday++.  Else, nothing.

Add one day.  Check if it is a weekend.  If it isn't, $workday++.  Else, nothing.

...

Until the $workday = the amount of days to work.

 

And you end up with the final date.

 

Of course, once you get the basic code down and working, you can work on optimizing it, such as adding two to the date when you hit Saturday.

 

Let me know if it makes any sense.

Link to comment
Share on other sites

Ok, I wrote it for you.  Let me know what you think.

 

<?php
	// d is the begin date in the format yyyy-mm-dd
	// hpd is the number of hours in a normal workday; 10 by default
	// h is the number of hours needed
	foreach ($_GET as $key => $value) {
		$_GET[$key] = htmlentities($value);
	}
	$begin_date = isset($_GET['d']) ? $_GET['d'] : '2007-01-01';
	$total_days = $_GET['h'] / (isset($_GET['hpd']) ? $_GET['hpd'] : 10);
	$days = 0;
	$temp_date = explode('-', $begin_date);
	while ($days < $total_days) {
		if ((date('D', mktime(0, 0, 0, $temp_date[1], $temp_date[2], $temp_date[0])) != 'Sat') && (date('D', mktime(0, 0, 0, $temp_date[1], $temp_date[2], $temp_date[0])) != 'Sun')) {
			$temp_date = explode('-', $begin_date);
			$begin_date = date('Y-m-d', mktime(0, 0, 0, $temp_date[1], $temp_date[2] + 1, $temp_date[0]));
			$days++;
		} else {
			$temp_date = explode('-', $begin_date);
			$begin_date = date('Y-m-d', mktime(0, 0, 0, $temp_date[1], $temp_date[2] + 1, $temp_date[0]));
		}
	}
	echo $begin_date;
?>

 

http://sneamia.net/junk/Jahnoes/?d=2007-01-01&h=101 for a test.

 

And sorry for the triple post.

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.