Loading index.php +12 −0 Original line number Diff line number Diff line Loading @@ -82,5 +82,17 @@ print $e->getMessage(); } ?> <h1>Function 6</h1> <?php print "<h2>Пятницы 13: </h2>"; try { foreach ($func->countFriday13(2024) as $date) { print $date->format("Y-m-d l") . "\n"; } } catch (Exception $e) { print $e->getMessage(); } ?> </body> </html> No newline at end of file src/Functions.php +18 −0 Original line number Diff line number Diff line Loading @@ -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")); } } } Loading
index.php +12 −0 Original line number Diff line number Diff line Loading @@ -82,5 +82,17 @@ print $e->getMessage(); } ?> <h1>Function 6</h1> <?php print "<h2>Пятницы 13: </h2>"; try { foreach ($func->countFriday13(2024) as $date) { print $date->format("Y-m-d l") . "\n"; } } catch (Exception $e) { print $e->getMessage(); } ?> </body> </html> No newline at end of file
src/Functions.php +18 −0 Original line number Diff line number Diff line Loading @@ -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")); } } }