Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 1.28 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Create an array of business days
  2. getWorkingDays("2008-01-01","2009-06-30");
  3.        
  4. Array
  5. (
  6.     [0] ="2008-01-01",
  7.     [1] ="2008-01-05",
  8.     [2] ="2008-01-06",
  9.     [3] ="2008-01-07",
  10.     [4] ="2008-01-08",
  11.     [5] ="2008-01-09",
  12.     [6] ="2008-01-12",
  13.     [7] ="2008-01-13",
  14.     [8] ="2008-01-14",
  15.     ...
  16. )
  17.        
  18. date('N', $dayStamp)
  19.        
  20. <?php
  21.  
  22.     function getWorkingDays($startDate, $endDate) {
  23.  
  24.         $businessDays = array();
  25.         $businessDaysInWeek = range(1,5);      // Only Monday to Friday
  26.  
  27.         // Decompose the provided dates.        
  28.         list($startYear, $startMonth, $startDay) = explode('-', $startDate);
  29.         list($endYear, $endMonth, $endDay) = explode('-', $endDate);
  30.  
  31.         // Create our start and end timestamps.
  32.         $startStamp = mktime(1, 1, 1, $startMonth, $startDay, $startYear);
  33.         $endStamp = mktime(1, 1, 1, $endMonth, $endDay, $endYear);
  34.  
  35.         // Check each day in turn.
  36.         for($loop=$startStamp; $loop<=$endStamp; $loop+=86400) {
  37.             if(in_array(date('N', $loop), $businessDaysInWeek)) {
  38.  
  39.                 // You'll also want to omit bank holidays, etc. in here.
  40.  
  41.                 $businessDays[] = date('Y-m-d', $loop);
  42.             }
  43.         }
  44.  
  45.         return $businessDays;
  46.     }
  47.  
  48.     print_r(getWorkingDays('2011-01-10', '2011-01-24'));
  49.  
  50. ?>