Jump to content

Need to Echo a list of the next 10 Business days


PhilipK

Recommended Posts

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

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.

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>";
   }
}

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 :)

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

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.