Advertisement
coasterka

03_WorkingDays.php

Aug 24th, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1. <?php
  2. date_default_timezone_set('Europe/Sofia');
  3.  
  4. // read the input
  5. $startDate = $_GET['dateOne'];
  6. $endDate = $_GET['dateTwo'];
  7. $holidays = array_filter(preg_split('/[\s]+/', $_GET['holidays']));
  8.  
  9. // pushing all dates in the range in the $allDates array
  10. $allDates = array();
  11. $start = strtotime($startDate);
  12. $end = strtotime($endDate);
  13. while ($start <= $end):
  14.     $allDates[] = date('d-m-Y', $start);
  15.     $start = strtotime('+1 days', $start);
  16. endwhile;
  17.  
  18. // array_intersect() finds the repeating values in two arrays
  19. // it returns an associative array with key -> value as follows:
  20. // key: the index of the repeating value; and
  21. // value: the repeating value (date)
  22. $existingHolidays = array_intersect($allDates, $holidays);
  23.  
  24. // remove/delete the official holidays from the $allDates array
  25. // array_search() searches the array for a given value
  26. // Returns the corresponding key if successful
  27. foreach ($existingHolidays as $holiday => $date):
  28.     $index = array_search($date, $allDates);
  29.     if($index !== FALSE):
  30.         unset($allDates[$index]);
  31.     endif;
  32. endforeach;
  33.  
  34. // check which of the remaining days are weekdays
  35. foreach ($allDates as $dateIndex => $date):
  36.     if (isWeekend($date)):
  37.         unset($allDates[$dateIndex]);
  38.     endif;
  39. endforeach;
  40.  
  41. // printing the result
  42. if (sizeof($allDates) > 0):
  43.     echo "<ol>";
  44.     foreach ($allDates as $workingDay):
  45.         echo "<li>$workingDay</li>";
  46.     endforeach;
  47.     echo "</ol>";
  48. else:
  49.     echo "<h2>No workdays</h2>";
  50. endif;
  51.  
  52. // the 'N' parameter takes values from 1 (for Monday) through 7 (for Sunday)
  53. function isWeekend($date) {
  54.     return (date('N', strtotime($date)) >= 6);
  55. }
  56. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement