PhilipK Posted June 8, 2011 Share Posted June 8, 2011 Hello I'm building a forum which has the user select a date. It would be nice if I could use php to echo out a list of the next 10 business days. What is the best way to approach this? Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/ Share on other sites More sharing options...
fugix Posted June 8, 2011 Share Posted June 8, 2011 you could add a for loop to loop through the next ten days, like so for (i=1; i<11; i++) { $future_date = mktime(0,0,0,date("m"),date("d")+i,date("Y")); echo date("Y/m/d", $future_date); } Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1226923 Share on other sites More sharing options...
Maq Posted June 8, 2011 Share Posted June 8, 2011 next 10 business days. Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1226924 Share on other sites More sharing options...
fugix Posted June 8, 2011 Share Posted June 8, 2011 next 10 business days. right you are, that code i posted would work for the next 10 days but not business days, thanks for catching my error maq. I'll write something else up and post later Edit: however, you could implement something similar to what i posted, except you will want to make an array of numbers representing the business days. 1= monday 2= tuesday 3= wednesday 4=thursday 5=friday 8=next monday.....etc Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1226927 Share on other sites More sharing options...
Maq Posted June 8, 2011 Share Posted June 8, 2011 The 'w' parameter for the date() function will return the will give you a number for the day of the week, from 0 for Sunday through 6 for Saturday. You will probably have to check to make sure the date is not 0 or 6. Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1226932 Share on other sites More sharing options...
matthew9090 Posted June 8, 2011 Share Posted June 8, 2011 you could use pagination Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1226958 Share on other sites More sharing options...
Pikachu2000 Posted June 8, 2011 Share Posted June 8, 2011 What does pagination have to do with generating a list of business days, exactly? Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1226961 Share on other sites More sharing options...
jcbones Posted June 8, 2011 Share Posted June 8, 2011 for($i = 0, $n = 10; $n > 0; $i++) { $d = date('w',strtotime("+$i day")); if($d == 0 || $d == 6) { continue; } echo date('l, m-d-Y',strtotime("+$i day")) . '<br />'; --$n; } Here is some code, do to some confusion on the topic. Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1226965 Share on other sites More sharing options...
PhilipK Posted June 8, 2011 Author Share Posted June 8, 2011 Thanks for the quick replies. I started working on it after the first few messages here is what I came up with... for($i=1; $i<11; $i++) { $future_date = mktime(0,0,0,date("m"),date("d")+$i,date("Y")); if(date("l", $future_date)!="Saturday" & date("l", $future_date)!="Sunday"){ echo "<li>".date("F d l", $future_date)."</li>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1226966 Share on other sites More sharing options...
PhilipK Posted June 8, 2011 Author Share Posted June 8, 2011 However I have not completed the task because I need to filter out holidays. I'm not sure how I'm going to go about this. I'm thinking find a list of holidays by day of the year and creating another if statement. Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1226967 Share on other sites More sharing options...
PhilipK Posted June 8, 2011 Author Share Posted June 8, 2011 Thanks everyone for your help. We have decided that the code is good to go. But if anyone is interested in taking a shot at filtering holidays I would love to hear your solution. Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1226974 Share on other sites More sharing options...
fugix Posted June 8, 2011 Share Posted June 8, 2011 you could go about it in the same manner that you filtered out weekends. just find the correct day and month Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1226975 Share on other sites More sharing options...
xyph Posted June 8, 2011 Share Posted June 8, 2011 Holidays are impossible, unless you want to predefine the days of the year. Things like Easter are different days each year. I guess you could specify the week number, then grab the first Monday of that week. I'll mess things around. Keep in mind any code I give you will be based off Canadian stat holidays Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1227035 Share on other sites More sharing options...
fugix Posted June 8, 2011 Share Posted June 8, 2011 Holidays are impossible, unless you want to predefine the days of the year. Things like Easter are different days each year. I guess you could specify the week number, then grab the first Monday of that week. I'll mess things around. Keep in mind any code I give you will be based off Canadian stat holidays if you were going to go about it using the logic in the code that you previously posted, you would of course need to update your code every year with the correct dates Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1227043 Share on other sites More sharing options...
jcbones Posted June 8, 2011 Share Posted June 8, 2011 Holidays are impossible, unless you want to predefine the days of the year. Things like Easter are different days each year. I guess you could specify the week number, then grab the first Monday of that week. I'll mess things around. Keep in mind any code I give you will be based off Canadian stat holidays PHP's answer to Easter, but since the OP wants all weekends excluded, Easter is a non-issue. Which might make this more in line with the OP's thinking. Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1227044 Share on other sites More sharing options...
xyph Posted June 8, 2011 Share Posted June 8, 2011 Here in BC, Canada, Good Friday is considered a stat holiday, while Easter Sunday and Easter Monday are not (though for some unions, Easter Monday is). Also, if July 1st (Canada day) falls on a Sunday, July 2nd is considered the stat. All of these factors come into play when dealing with calculating holidays. Pretty easy if you don't mind telling it what dates are holidays. <?php $weekends = array(0,6); // Days of the week $holidays = array( '01-01-2011' => 'New Years', '22-04-2011' => 'Good Friday', '23-05-2011' => 'Victoria Day', '01-07-2011' => 'Canada Day', '01-08-2011' => 'B.C. Day', '05-09-2011' => 'Labour Day', '10-10-2011' => 'Thanksgiving', '11-11-2011' => 'Remembrance Day', '25-12-2011' => 'Christmas Day' ); $now = mktime( 1,0,0,6,20,2011 ); // Temp to include a holiday in results // $now = time(); $day = date('w',$now); // Get a numerical representation of the current day of the week $length = 10; // How many business days for($i=0;$i<$length;) { // Start looping until we've got 10 business days // Grab the date in DD-MM-YYYY format for holiday checking, and the current day of the week list($date,$dayOfWeek) = explode( ',',date('d-m-Y,l',$now) ); // Check to see if the current day is a weekend if( in_array($day,$weekends) ) echo $date.' is a weekend'; // Check to see if the date is a holiday elseif( array_key_exists($date,$holidays) ) echo $date.' is '. $holidays[$date]; // If neither, it's gotta be a business day else { echo $date.' is a business day'; $i++; // Increase counter by a day } if( $day == 6 ) $day = 0; // If you're on a Saturday (6) go back to Sunday (0) else $day++; // Otherwise, increase our day of the week by one $now += 86400; // Increase the timestamp counter by 24 hours (60*60*24) echo ' ('.$dayOfWeek.')<br>'; // Add a linebreak to make the output pretty } ?> IMO, you're still not doing it the best way, from an efficiency standpoint. You want to avoid having to use the date() function in a loop if you can. For example, if I just wanted to know the DATE in 10 business days, I would do something like this. Code coming in a sec Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1227163 Share on other sites More sharing options...
xyph Posted June 8, 2011 Share Posted June 8, 2011 edit - After re-reading the original post, you pretty much have to use date() in a loop. Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1227200 Share on other sites More sharing options...
matthew9090 Posted June 9, 2011 Share Posted June 9, 2011 What does pagination have to do with generating a list of business days, exactly? i thought you could store the buisiness days in the database and use pagination to display the next lot of them Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1227292 Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 Technically you could use pagination to cycle through the list. However you posted a 4 word sentence that did not point the op in the right direction of his actual question nor even give him an example of pagination at the very least. Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1227306 Share on other sites More sharing options...
Maq Posted June 9, 2011 Share Posted June 9, 2011 I don't get how pagination has anything to do with this? Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1227471 Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 I don't get how pagination has anything to do with this? my thoughts exactly Maq Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1227480 Share on other sites More sharing options...
Maq Posted June 9, 2011 Share Posted June 9, 2011 I don't get how pagination has anything to do with this? my thoughts exactly Maq Oh, I didn't read the some of the previous posts, my bad. Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1227482 Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 I don't get how pagination has anything to do with this? my thoughts exactly Maq Oh, I didn't read the some of the previous posts, my bad. oh, you were wondering why I was talking about pagination?..heh Quote Link to comment https://forums.phpfreaks.com/topic/238776-need-to-echo-a-list-of-the-next-10-business-days/#findComment-1227493 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.