Jahnoes Posted September 5, 2007 Share Posted September 5, 2007 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 />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/68122-begin-date-and-end-date-but-no-weekends/ Share on other sites More sharing options...
Jessica Posted September 5, 2007 Share Posted September 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/68122-begin-date-and-end-date-but-no-weekends/#findComment-342418 Share on other sites More sharing options...
Jahnoes Posted September 5, 2007 Author Share Posted September 5, 2007 Yes, the work week is then 5 days, but what does the my script doing wrong?. Quote Link to comment https://forums.phpfreaks.com/topic/68122-begin-date-and-end-date-but-no-weekends/#findComment-342420 Share on other sites More sharing options...
Jessica Posted September 5, 2007 Share Posted September 5, 2007 Uhm, as you said, it's not accounting for over one week. You're only checking for the end date falling on a weekend, not any of the weekends inbetween. Quote Link to comment https://forums.phpfreaks.com/topic/68122-begin-date-and-end-date-but-no-weekends/#findComment-342425 Share on other sites More sharing options...
Jahnoes Posted September 5, 2007 Author Share Posted September 5, 2007 Hmm, well i look for a solution, but i am trying it for 2 days now ;( Quote Link to comment https://forums.phpfreaks.com/topic/68122-begin-date-and-end-date-but-no-weekends/#findComment-342428 Share on other sites More sharing options...
Jessica Posted September 5, 2007 Share Posted September 5, 2007 I guess it's impossible. Too bad. Quote Link to comment https://forums.phpfreaks.com/topic/68122-begin-date-and-end-date-but-no-weekends/#findComment-342445 Share on other sites More sharing options...
sneamia Posted September 5, 2007 Share Posted September 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/68122-begin-date-and-end-date-but-no-weekends/#findComment-342448 Share on other sites More sharing options...
sneamia Posted September 5, 2007 Share Posted September 5, 2007 And I can't edit my post, so I guess double posting is the only option. Why is "60 * 60 * 24" there? Doesn't that convert from days to seconds? Quote Link to comment https://forums.phpfreaks.com/topic/68122-begin-date-and-end-date-but-no-weekends/#findComment-342479 Share on other sites More sharing options...
sneamia Posted September 6, 2007 Share Posted September 6, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/68122-begin-date-and-end-date-but-no-weekends/#findComment-342546 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.