Skip to content

Instantly share code, notes, and snippets.

@tonda13
Last active March 15, 2019 20:24
Show Gist options
  • Select an option

  • Save tonda13/7b95b469243124af031d to your computer and use it in GitHub Desktop.

Select an option

Save tonda13/7b95b469243124af031d to your computer and use it in GitHub Desktop.
Get friday 13th
<?php
/**
* @param int $fromYear Year from you want started search fridays 13th
* @param int $toYear Year up to you want end with search fridays 13th
*
* @return DateTimeImmutable[]
*/
function getFridays13th(int $fromYear, int $toYear) : array {
$fridays = [];
$startDate = new DateTimeImmutable("{$fromYear}-01-01 Friday");
$endDate = new DateTimeImmutable("{$toYear}-12-31");
$interval = new DateInterval('P7D');
foreach(new DatePeriod($startDate, $interval, $endDate) as $d) {
if (intval($d->format('j')) == 13) {
$fridays[] = $d;
}
}
return $fridays;
}
$fridays = getFridays13th(2019, 2025);
array_walk($fridays, function($item) {
echo $item->format('d. m. Y') . PHP_EOL;
});
13. 09. 2019
13. 12. 2019
13. 03. 2020
13. 11. 2020
13. 08. 2021
13. 05. 2022
13. 01. 2023
13. 10. 2023
13. 09. 2024
13. 12. 2024
13. 06. 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment