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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.