Created
January 14, 2022 13:28
-
-
Save tobiasroeder/bffbca519e0b949a9cdc5898ac488149 to your computer and use it in GitHub Desktop.
Get the date for the first advent.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param string $date - [YYYY-MM-DD] | |
* @return int | |
*/ | |
function getWeekday( string $date ):int { | |
// 'w' 0 (for Sunday) through 6 (for Saturday) | |
$result = (int) date('w', strtotime($date)); | |
return $result; | |
} | |
/** | |
* @param string $christmasEve - [YYYY-MM-DD] | |
* @param string $format - 'd.m.Y' | |
* @return string | |
*/ | |
function get1stAdvent( string $christmasEve, string $format = 'd.m.Y' ):string { | |
$datetime = new DateTime($christmasEve); | |
$diff = getWeekday($christmasEve) + 21; | |
$datetime->modify('-' . $diff . ' day'); | |
return $datetime->format($format); | |
} | |
/** | |
* Demo - get the 1st advent for the next 10 years | |
*/ | |
echo '<pre>'; | |
for ($i = 0; $i <= 10; $i++): | |
$year = date('Y') + $i; | |
echo get1stAdvent($year . '-12-24', 'd.m.Y'); | |
echo '<br>'; | |
endfor; | |
echo '</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment