note 101379 added to function.date

From: Date: Mon, 13 Dec 2010 18:06:29 +0000
Subject: note 101379 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-174886@lists.php.net to get a copy of this message
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--; } return $nwd; } $start = strtotime("1 January 2010"); $end = strtotime("13 December 2010"); // 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: <?php function workdaysinyear($y) { $j1 = mktime(0,0,0,1,1,$y); if (date("L", $j1)) { if (date("N", $j1) == 6) return 260; elseif (date("N", $j1) == 5 or date("N", $j1) == 7) return 261; else return 262; } else { if (date("N", $j1) == 6 or date("N", $j1) == 7) return 260; else return 261; } } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 208.176.90.90 ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- https://master.php.net/note/edit/101379 Del: integrated -- https://master.php.net/note/delete/101379/integrated Del: useless -- https://master.php.net/note/delete/101379/useless Del: bad code -- https://master.php.net/note/delete/101379/bad+code Del: spam -- https://master.php.net/note/delete/101379/spam Del: non-english -- https://master.php.net/note/delete/101379/non-english Del: in docs -- https://master.php.net/note/delete/101379/in+docs Del: other reasons-- https://master.php.net/note/delete/101379 Reject -- https://master.php.net/note/reject/101379 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#174886) next »