If you have a problem with the different time zone, this is the solution for that.<?php// first line of PHP$defaultTimeZone='UTC';if(date_default_timezone_get()!=$defaultTimeZone)) date_default_timezone_set($defaultTimeZone);

Most spreadsheet programs have a rather nice little built-in function called NETWORKDAYS to calculate the number of business days (i.e. Monday-Friday, excluding holidays) between any two given dates. I couldn't find a simple way to do that in PHP, so I threw this together. It replicates the functionality of OpenOffice's NETWORKDAYS function - you give it a start date, an end date, and an array of any holidays you want skipped, and it'll tell you the number of business days (inclusive of the start and end days!) between them.

I've tested it pretty strenuously but date arithmetic is complicated and there's always the possibility I missed something, so please feel free to check my math.

The function could certainly be made much more powerful, to allow you to set different days to be ignored (e.g. "skip all Fridays and Saturdays but include Sundays") or to set up dates that should always be skipped (e.g. "skip July 4th in any year, skip the first Monday in September in any year"). But that's a project for another time.

<?php

function networkdays($s, $e, $holidays = array()) {// If the start and end dates are given in the wrong order, flip them. if ($s > $e) return networkdays($e, $s, $holidays);

// Find the ISO-8601 day of the week for the two dates.$sd = date("N", $s);$ed = date("N", $e);

// Find the number of weeks between the dates.$w = floor(($e - $s)/(86400*7)); # Divide the difference in the two times by seven days to get the number of weeks.if ($ed >= $sd) { $w--; } # If the end date falls on the same day of the week or a later day of the week than the start date, subtract a week.

// Calculate net working days.$nwd = max(6 - $sd, 0); # If the start day is Saturday or Sunday, add zero, otherewise add six minus the weekday number.$nwd += min($ed, 5); # If the end day is Saturday or Sunday, add five, otherwise add the weekday number.$nwd += $w * 5; # Add five days for each week in between.

// Iterate through the array of holidays. For each holiday between the start and end dates that isn't a Saturday or a Sunday, remove one day.foreach ($holidays as $h) {$h = strtotime($h); if ($h > $s && $h < $e && date("N", $h) < 6)$nwd--; }

// Add as many holidays as desired.$holidays = array();$holidays[] = "4 July 2010"; // Falls on a Sunday; doesn't affect count$holidays[] = "6 September 2010"; // Falls on a Monday; reduces count by one

echo networkdays($start, $end, $holidays); // Returns 246

?>

Or, if you just want to know how many work days there are in any given year, here's a quick function for that one:

I've been flicking through the comments looking for some succinct date code and have noticed an alarming number of questions and over-burdened examples related to date mathematics. One of the most useful skills you can utilize when performing date math is taking full advantage of the UNIX timestamp. The UNIX timestamp was built for this kind of work.

An example of this relates to a comment made by james at bandit-dot-co-dot-en-zed. James was looking for a way to calculate the number of days which have passed since a certain date. Rather than using mktime() and a loop, James can subtract the current timestamp from the timestamp of the date in question and divide that by the number of seconds in a day:<?php$days = floor((time() - strtotime("01-Jan-2006"))/86400);print("$days days have passed.\n");?>

Another usage could find itself in a class submitted by Kyle M Hall which aids in the creation of timestamps from the recent past for use with MySQL. Rather than the looping and fine tuning of a date, Kyle can use the raw UNIX timestamps (this is untested code):<?php$ago = 14; // days$timestamp = time() - ($ago * 86400);?>

Hopefully these two examples of "UNIX-style" timestamp usage will help those finding date mathematics more elusive than it should be.

The following function will return the date (on the Gregorian calendar) for Orthodox Easter (Pascha). Note that incorrect results will be returned for years less than 1601 or greater than 2399. This is because the Julian calendar (from which the Easter date is calculated) deviates from the Gregorian by one day for each century-year that is NOT a leap-year, i.e. the century is divisible by 4 but not by 10. (In the old Julian reckoning, EVERY 4th year was a leap-year.)

This algorithm was first proposed by the mathematician/physicist Gauss. Its complexity derives from the fact that the calculation is based on a combination of solar and lunar calendars.

Note that some formatting options are different from MySQL.For example using a 24 hour notation without leading zeros is the option '%G' in PHP but '%k' in MySQL.When using dynamically generated date formatting string, be careful to generate the correct options for either PHP or MySQL.

While this will work for the majority of years it will not work on years that are multiples of 100 but not multiples of 400 i.e.(2100).A function not using php's date() function that will also account for this small anomaly in leap years:

While is_leapyear_working will not return true for the few non-leap years divisible by four I couldn't tell you if this is more or less efficient than using php's date() as an even earlier poster suggested:

In order to define leap year you must considre not only that year can be divide by 4!

The correct alghoritm is:

if (year is not divisible by 4) then (it is a common year)else if (year is not divisible by 100) then (it is a leap year)else if (year is not divisible by 400) then (it is a common year)else (it is a leap year)

If you see the number 86400 in a date calculation, think very hard before deciding that it is correct. Not all days have 86,400 seconds in them. In many places, some days have only 82,800 seconds and some have 90,000. Occasionally, there are even fewer or even more.

Assuming that now plus 86,400 seconds is equivalent to some time tomorrow can sometimes be wrong. It might actually be the day after tomorrow or still today.

Thanks to tcasparr at gmail dot com for the great idea (at least for me) ;)I changed the code a little to replicate the functionality of date_parse_from_format, once I don't have PHP 5.3.0 yet. This might be useful for someone. Hope you don't mind changing your code tcasparr at gmail dot com.

In order to define leap year you must considre not only that year can be divide by 4!

The correct alghoritm is:

if (year is not divisible by 4) then (it is a common year)else if (year is not divisible by 100) then (it is a leap year)else if (year is not divisible by 400) then (it is a common year)else (it is a leap year)

In order to define leap year you must considre not only that year can be divide by 4!

The correct alghoritm is:

if (year is not divisible by 4) then (it is a common year)else if (year is not divisible by 100) then (it is a leap year)else if (year is not divisible by 400) then (it is a common year)else (it is a leap year)

Since PHP 5.6.23 en 7.0.8 the support for requesting the weeknumber for a given date, where the first day of the week is Sunday, has been removed. For those of us still needing it, here is simple hack to get the job done.

I am glad and wonder about the topic creation. I am visiting this page from a long time ago. This is totally perspective clear ideas. This is sure that the information you shared is clearly identive and fabulous for keeping it in minds. http://crackswar.com/audacity-pro-crack/

I am glad and wonder about the topic creation. I am visiting this page from a long time ago. This is totally perspective clear ideas. This is sure that the information you shared is clearly identive and fabulous for keeping it in minds. http://crackswar.com/audacity-pro-crack/