matthewst Posted April 2, 2008 Share Posted April 2, 2008 I need to calculate 14 working days after a given date. I found this on php.net: <?php $holidayList = array("04-03-2008","07-03-2008"); $j = $i = 1; ///changed to 14 while($i <= 5) ///it didn't work { $day = strftime("%A",strtotime("+$j day")); $tmp = strftime("%d-%m-%Y",strtotime("+$j day")); if($day != "Sunday" and $day != "Saturday" and !in_array($tmp, $holidayList)) { $i = $i + 1; $j = $j + 1; } else $j = $j + 1; } $j = $j -1; echo strftime("%A, %d-%m-%Y",strtotime("+$j day")); ?> The author designed it to calculate 5 working days. When I change the 5 to 14 it calculates 22 working days. Link to comment https://forums.phpfreaks.com/topic/99199-how-do-i-calculate-the-number-of-working-days-from-a-given-date/ Share on other sites More sharing options...
paul2463 Posted April 2, 2008 Share Posted April 2, 2008 have just tested it on my system todays date Wednesday, 02-04-2008 function run with 5 days reads out Wednesday, 09-04-2008 amended to read 10 working days (two weeks) it reads Wednesday, 16-04-2008 changed to 14 ( your problem days) it reads Tuesday, 22-04-2008 which is correct if you discount weekends as the function does Link to comment https://forums.phpfreaks.com/topic/99199-how-do-i-calculate-the-number-of-working-days-from-a-given-date/#findComment-507571 Share on other sites More sharing options...
matthewst Posted April 2, 2008 Author Share Posted April 2, 2008 How do I get it to go 14 days? Lets say the given date is 3/1/2008. It should calculate 3/20/2008. Mine keeps going to 4/23/2008. I'm using this to get the given date into the equation: <?php $holidayList = array("04-03-2008","07-03-2008"); $j = $i = 1; ///changed to 14 while($i <= 5) ///it didn't work { $day = $given_date; $day = strftime("%A",strtotime("+$j day")); // then the rest of the code Link to comment https://forums.phpfreaks.com/topic/99199-how-do-i-calculate-the-number-of-working-days-from-a-given-date/#findComment-507656 Share on other sites More sharing options...
matthewst Posted April 2, 2008 Author Share Posted April 2, 2008 Getting closer. $given_date = "3/1/2008"; $day = strftime("%A",strtotime("+$j $given_date")); gets me 4/16/2008 Echoing $day = Friday 3/1/2008 was a Saturday I changed the last bit of code to this. $working_days = strftime("%A, %d-%m-%Y",strtotime("+$j day")); Echoing $working_days = 04/16/2008 Link to comment https://forums.phpfreaks.com/topic/99199-how-do-i-calculate-the-number-of-working-days-from-a-given-date/#findComment-507741 Share on other sites More sharing options...
paul2463 Posted April 3, 2008 Share Posted April 3, 2008 I h ave made some changes to your code, I have broken it down a bit more to help me go through it and I have put some echo statements is to show whats happening <?php $holidayList = array("04-03-2008","07-03-2008"); $j = $i = 0; //changed this to 0 or you were always starting one day ahead $given_date = "3/1/2008"; $tmp1 = strtotime($given_date); //worked out a timstamp to start with while($i < 14) { $tmp2 = strtotime("+$j day", $tmp1); $day = strftime("%A",$tmp2); echo strftime("%d-%m-%Y",$tmp2); echo " $day"; $tmp = strftime("%d-%m-%Y",$tmp2); if(($day != "Sunday") && ($day != "Saturday" )&&(!in_array($tmp, $holidayList))) { $i = $i + 1; $j = $j + 1; echo "<BR>"; } else { $j = $j + 1; echo " - weekend! or Holiday! <BR>"; } } $j = $j -1; $newdate = strtotime("+$j day",$tmp1); echo strftime("%A, %d-%m-%Y",$newdate); ?> and it printed out the following which I believe is correct for your number of days and holiday list 01-03-2008 Saturday - weekend! or Holiday! 02-03-2008 Sunday - weekend! or Holiday! 03-03-2008 Monday 04-03-2008 Tuesday - weekend! or Holiday! 05-03-2008 Wednesday 06-03-2008 Thursday 07-03-2008 Friday - weekend! or Holiday! 08-03-2008 Saturday - weekend! or Holiday! 09-03-2008 Sunday - weekend! or Holiday! 10-03-2008 Monday 11-03-2008 Tuesday 12-03-2008 Wednesday 13-03-2008 Thursday 14-03-2008 Friday 15-03-2008 Saturday - weekend! or Holiday! 16-03-2008 Sunday - weekend! or Holiday! 17-03-2008 Monday 18-03-2008 Tuesday 19-03-2008 Wednesday 20-03-2008 Thursday 21-03-2008 Friday 22-03-2008 Saturday - weekend! or Holiday! 23-03-2008 Sunday - weekend! or Holiday! 24-03-2008 Monday Monday, 24-03-2008 Link to comment https://forums.phpfreaks.com/topic/99199-how-do-i-calculate-the-number-of-working-days-from-a-given-date/#findComment-508320 Share on other sites More sharing options...
matthewst Posted April 3, 2008 Author Share Posted April 3, 2008 <?php $holidayList = array("04-03-2008","07-03-2008"); $j = $i = 0; //changed this to 0 or you were always starting one day ahead $given_date = "3/1/2008"; $tmp1 = strtotime($given_date); //worked out a timstamp to start with while($i < 14) { $tmp2 = strtotime("+$j day", $tmp1); $day = strftime("%A",$tmp2); echo strftime("%d-%m-%Y",$tmp2); $tmp = strftime("%d-%m-%Y",$tmp2); if(($day != "Sunday") && ($day != "Saturday" )&&(!in_array($tmp, $holidayList))) { $i = $i + 1; $j = $j + 1; } else { $j = $j + 1; } } $j = $j -1; $newdate = strtotime("+$j day",$tmp1); $working_days = strftime("%A, %d-%m-%Y",$newdate); ?> Works perfectly!! Thanks!! Link to comment https://forums.phpfreaks.com/topic/99199-how-do-i-calculate-the-number-of-working-days-from-a-given-date/#findComment-508646 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.