Venciity

working days 90/100 points

Aug 24th, 2014
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.99 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title></title>
  5.     <style>
  6.         label, textarea, input {
  7.             display: block;
  8.         }
  9.         textarea {
  10.             height: 200px;
  11.         }
  12.     </style>
  13. </head>
  14. <body>
  15. <form method="get" action="">
  16.     <label>Start date:</label>
  17.     <input type="text" name="dateOne" value="17-12-2014"/>
  18.     <label>End date:</label>
  19.     <input type="text" name="dateTwo" value="31-12-2014"/>
  20.     <label>Holidays:</label>
  21.     <textarea name="holidays">31-12-2014
  22.         24-12-2014
  23.         08-12-2014</textarea>
  24.     <input type="submit" />
  25. </form>
  26. <?php
  27.     if (isset($_GET['dateOne']) && isset($_GET['dateTwo']) && isset($_GET['holidays'])) {
  28.         date_default_timezone_set('Europe/Sofia');
  29.         $dateOne = strtotime($_GET['dateOne']);
  30.         $dateTwo = strtotime($_GET['dateTwo']);
  31.         $holidaysString = $_GET['holidays'];
  32.         $holidays = preg_split("/\\s+/", $holidaysString, -1, PREG_SPLIT_NO_EMPTY);
  33.         //var_dump($holidays);
  34.  
  35.         $AllDays = floor(($dateTwo - $dateOne)/(60*60*24));
  36.         $AllDays += 1; // including last date
  37.         //echo $AllDays;
  38.  
  39.         $workDays = [];
  40.         $currentDate = $dateOne;
  41.         //echo $currentDate;
  42.  
  43.         for ($i = 0; $i < $AllDays; $i++) {
  44.             $dayOfWeek = date('N',$currentDate);
  45.             if (($dayOfWeek > 5) || in_array(date("d-m-Y", $currentDate), $holidays)) {
  46.                 $currentDate = strtotime('+1 day', $currentDate);
  47.             }
  48.             else {
  49.                 $workDays[] = $currentDate;
  50.                 $currentDate = strtotime('+1 day', $currentDate);
  51.             }
  52.         }
  53.  
  54.         if (count($workDays) == 0) {
  55.             echo "<h2>No workdays</h2>";
  56.         }
  57.         else {
  58.             echo "<ol>";
  59.             for ($i = 0; $i < count($workDays); $i++) {
  60.                 $date = date("d-m-Y", $workDays[$i]);
  61.                 echo "<li>$date</li>";
  62.             }
  63.             echo "</ol>";
  64.         }
  65.     }
  66. ?>
  67. </body>
  68. </html>
Advertisement
Add Comment
Please, Sign In to add comment