note 129906 added to function.easter-date

From: Date: Mon, 09 Dec 2024 12:24:12 +0000
Subject: note 129906 added to function.easter-date
Groups: php.notes 
Request: Send a blank email to php-notes+get-221711@lists.php.net to get a copy of this message
<?php class DateUtils { /** * Determines if a given date is a public holiday in european France, excluding Alsace-Moselle. * * @param DateTime $date The date to check. * @return bool True if the date is a public holiday, False otherwise. */ public static function isFrenchHoliday(DateTime $date): bool { // Convert the date to check to Europe/Paris time // Note: This can affect the day or even the year compared to UTC time $dateToCheck = clone $date; $dateToCheck->setTimezone(new DateTimeZone('Europe/Paris')); $year = $dateToCheck->format('Y'); $easterDate = new DateTime('@'.easter_date($year), new DateTimeZone('UTC')); // Note: Without DateTimeZone('UTC'), the date created from the timestamp is still in UTC, but it's a good practice to specify it explicitly. /* easter_date() returns a timestamp for Easter that is convertible to 0:00 in local time, but based on a UTC date/time! For example, for 2024-04-01, a DateTime created with the timestamp will be on 2024-03-31 23:00 UTC in a system with UTC+1 in order to get the correct result (at 0:00) in local time (UTC+1) */ $easterDate->setTimezone(new DateTimeZone('Europe/Paris')); $holidays = [ // Fixed holidays '01-01', // New Year's Day '01-05', // Lab0r Day '08-05', // Vict0ry in Europe Day '14-07', // Bastille Day '15-08', // Assumption of Mary '01-11', // All Saints' Day '11-11', // Armistice Day '25-12', // Christmas Day // Movable holidays // Note: Easter (Sunday) is not listed as a public French holiday (clone $easterDate)->modify('+1 day')->format('d-m'), // Easter Monday (Easter + 1 day) (clone $easterDate)->add(new DateInterval('P39D'))->format('d-m'), // Ascension (Easter + 39 days) // Pentecost is celebrated on the seventh Sunday after Easter, i.e., 7x7 days, or 49 days... // Note: Pentecost is not listed as a public French holiday (clone $easterDate)->add(new DateInterval('P50D'))->format('d-m'), // Whit Monday (Easter + 50 days) ]; // Note: Alsace-Moselle has two additional public holidays. // Note: In Guadeloupe, Guyana, Martinique, Mayotte, Réunion, Saint-Barthélemy, and Saint-Martin, the abolition of slavery is commemorated as a public French holiday https://www.legifrance.gouv.fr/codes/section_lc/LEGITEXT000006072050/LEGISCTA000006160771/ return in_array($dateToCheck->format('d-m'), $joursFeries); } } ?> <?php class DateUtilsTest extends TestCase { public function testIsFrenchHolidayReturnsFalseForNonHoliday() { $date = new DateTime('2023-09-27'); $this->assertFalse(DateUtils::isFrenchHoliday($date)); } /** * @dataProvider datesFor2024 */ public function testIsFrenchHolidayReturnsTrueFor2024($dateString) { $date = new DateTime($dateString); $this->assertTrue(DateUtils::isFrenchHoliday($date)); } public function datesFor2024() { // source: https://www.service-public.fr/particuliers/actualites/A15406 return [ ['2024-01-01'], // New Year's Day ['2024-04-01'], // Easter Monday ['2024-05-01'], // Labor Day ['2024-05-08'], // Victory in Europe Day ['2024-05-09'], // Ascension ['2024-05-20'], // Whit Monday ['2024-07-14'], // Bastille Day ['2024-08-15'], // Assumption of Mary ['2024-11-01'], // All Saints' Day ['2024-11-11'], // Armistice Day ['2024-12-25'], // Christmas Day ]; } // public function testIsFrenchHolidayHandlesDatesAroundMidnight($dateString, $fromTimeZone, $toTimeZone, $expected) // ... } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 213.144.203.250) ---- Manual Page -- https://php.net/manual/en/function.easter-date.php Edit -- https://main.php.net/note/edit/129906 Del: integrated -- https://main.php.net/note/delete/129906/integrated Del: useless -- https://main.php.net/note/delete/129906/useless Del: bad code -- https://main.php.net/note/delete/129906/bad+code Del: spam -- https://main.php.net/note/delete/129906/spam Del: non-english -- https://main.php.net/note/delete/129906/non-english Del: in docs -- https://main.php.net/note/delete/129906/in+docs Del: other reasons-- https://main.php.net/note/delete/129906 Reject -- https://main.php.net/note/reject/129906 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221711) next »