Skip to content

Instantly share code, notes, and snippets.

@tobiasroeder
Created January 14, 2022 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiasroeder/bffbca519e0b949a9cdc5898ac488149 to your computer and use it in GitHub Desktop.
Save tobiasroeder/bffbca519e0b949a9cdc5898ac488149 to your computer and use it in GitHub Desktop.
Get the date for the first advent.
<?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