diff --git a/index.php b/index.php index d721329cb08fba3003f45eef28ef768da0f50d22..4573d8d9868f489c6e410e4b02854700dbe1536e 100644 --- a/index.php +++ b/index.php @@ -82,5 +82,17 @@ print $e->getMessage(); } ?> + +

Function 6

+ Пятницы 13: "; + try { + foreach ($func->countFriday13(2024) as $date) { + print $date->format("Y-m-d l") . "\n"; + } + } catch (Exception $e) { + print $e->getMessage(); + } + ?> \ No newline at end of file diff --git a/src/Functions.php b/src/Functions.php index e3c266eacc18b0ef2e0d01c5747e31d6f3734e19..f7b52aa43644e535ee393f3fc3973a396378c55e 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -151,5 +151,23 @@ class Functions $dateInterval = date_diff(new DateTimeImmutable($endYear), $date); return (int)$dateInterval->format("%a") + 1; } + + /** + * Вернет все пятницы 13 в году + * @param int $year год, в котором необходимо произвести расчет + * @return DateTimeImmutable[] + * @throws Exception + */ + + public function countFriday13(int $year): iterable + { + $startDate = new DateTime("$year-01-01 Friday"); + ++$year; + $endDate = new DateTime("$year-01-01"); + $interval = new DateInterval('P7D'); + foreach (new DatePeriod($startDate, $interval, $endDate) as $day) { + yield new DateTimeImmutable($day->format("Y-m-d")); + } + } }