COUNT DAYS -3 - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of COUNT DAYS -3.php

  1. <?php
  2.  
  3. use DateTime;
  4. use DateTimeZone;
  5. use Exception;
  6.  
  7. /**
  8.  * Permet de compter le nombre de jours ouvrés pour une période donnée.
  9.  */
  10. class WorkingDayCounter
  11. {
  12.     /**
  13.      * @var int
  14.      */
  15.     const SATURDAY_INDEX = 6;
  16.  
  17.     /**
  18.      * @var int
  19.      */
  20.     const SECOND_DAY_OF_THE_MONTH = 2;
  21.  
  22.     /**
  23.      * @var array
  24.      */
  25.     static $easter_dates = [
  26.         1990 => 640130400,
  27.         1991 => 670374000,
  28.         1992 => 703634400,
  29.         1993 => 734479200,
  30.         1994 => 765324000,
  31.         1995 => 797983200,
  32.         1996 => 828828000,
  33.         1997 => 859676400,
  34.         1998 => 892332000,
  35.         1999 => 923176800,
  36.         2000 => 956440800,
  37.         2001 => 987285600,
  38.         2002 => 1017529200,
  39.         2003 => 1050789600,
  40.         2004 => 1081634400,
  41.         2005 => 1111878000,
  42.         2006 => 1145138400,
  43.         2007 => 1175983200,
  44.         2008 => 1206226800,
  45.         2009 => 1239487200,
  46.         2010 => 1270332000,
  47.         2011 => 1303596000,
  48.         2012 => 1333836000,
  49.         2013 => 1364684400,
  50.         2014 => 1397944800,
  51.         2015 => 1428184800,
  52.         2016 => 1459033200,
  53.         2017 => 1492293600,
  54.         2018 => 1522533600,
  55.         2019 => 1555797600,
  56.         2020 => 1586642400,
  57.         2021 => 1617487200,
  58.         2022 => 1650146400,
  59.         2023 => 1680991200,
  60.         2024 => 1711839600,
  61.         2025 => 1745100000,
  62.         2026 => 1775340000,
  63.         2027 => 1806188400,
  64.         2028 => 1839448800,
  65.         2029 => 1869688800,
  66.         2030 => 1902952800,
  67.     ];
  68.  
  69.     /**
  70.      * @param $year
  71.      * @return array
  72.      */
  73.     public static function getHolidaysDaysOfAGivenYear(int $year): array
  74.     {
  75.         $holidays = [];
  76.  
  77.         // Liste des jours feriés.
  78.         $holidays[] = '1_1_' . $year; // Jour de l'an
  79.         $holidays[] = '1_5_' . $year; // Fete du travail
  80.         $holidays[] = '8_5_' . $year; // Victoire 1945
  81.         $holidays[] = '14_7_' . $year; // Fête nationale
  82.         $holidays[] = '15_8_' . $year; // Assomption
  83.         $holidays[] = '1_11_' . $year; // Toussaint
  84.         $holidays[] = '11_11_' . $year; // Armistice 1918
  85.         $holidays[] = '25_12_' . $year; // Noel
  86.  
  87.         // Récupération de paques. Permet ensuite d'obtenir le jour de l'ascension et celui de la pentecote.
  88.         $easter = self::$easter_dates[$year];
  89.         $holidays[] = date('j_n_' . $year, $easter + 86400); // Paques
  90.         $holidays[] = date('j_n_' . $year, $easter + (86400 * 39)); // Ascension
  91.         $holidays[] = date('j_n_' . $year, $easter + (86400 * 50)); // Pentecote
  92.  
  93.         return $holidays;
  94.     }
  95.  
  96.     /**
  97.      * @param int $year
  98.      * @param int $month
  99.      * @return array
  100.      */
  101.     public static function getHolidaysDaysAsDateTimeForAGivenYearAndMonth(int $year, int $month): array
  102.     {
  103.         $holidays = self::getHolidaysDaysOfAGivenYear($year);
  104.         foreach ($holidays as $k => $holiday) {
  105.             if (!preg_match("/^(.*)_{$month}_(.*)$/", $holiday)) {
  106.                 unset($holidays[$k]);
  107.             }
  108.         }
  109.  
  110.         return $holidays;
  111.     }
  112.  
  113.     /**
  114.      * @param int $nbOpenDaysToSubstract
  115.      * @return DateTime
  116.      * @throws Exception
  117.      */
  118.     public function getLastDayOfCurrentDateWithoutNbOpenDaysGiven(int $nbOpenDaysToSubstract): DateTime
  119.     {
  120.  
  121.         $holidaysOfAGivenYearAndMonth = self::getHolidaysDaysAsDateTimeForAGivenYearAndMonth(date('Y'), date('n'));
  122.         $lastDayOfMonthWithoutOpenDaysGiven = new DateTime("last day of this month", new DateTimeZone("Europe/Amsterdam"));
  123.  
  124.         while ($nbOpenDaysToSubstract > 0) {
  125.             /// If the date isn't a sunday/saturday + no holliday => decrement
  126.             if ($lastDayOfMonthWithoutOpenDaysGiven->format('N') < self::SATURDAY_INDEX
  127.                 && !in_array($lastDayOfMonthWithoutOpenDaysGiven->format('j_n_Y'), $holidaysOfAGivenYearAndMonth)) {
  128.                 $nbOpenDaysToSubstract--;
  129.             }
  130.  
  131.             if($lastDayOfMonthWithoutOpenDaysGiven->format('d') < self::SECOND_DAY_OF_THE_MONTH) {
  132.                 return $lastDayOfMonthWithoutOpenDaysGiven;
  133.             }
  134.  
  135.             $lastDayOfMonthWithoutOpenDaysGiven = $lastDayOfMonthWithoutOpenDaysGiven->modify('-1 day');
  136.         }
  137.  
  138.         return $lastDayOfMonthWithoutOpenDaysGiven;
  139.     }
  140. }
  141.  
  142. $difference = 3;
  143. $service = new WorkingDayCounter();
  144. $resultat = $service->getLastDayOfCurrentDateWithoutNbOpenDaysGiven($difference);
  145.  
  146. var_dump($resultat);
File Description
  • COUNT DAYS -3
  • PHP Code
  • 13 Jan-2020
  • 4.28 Kb
You can Share it: