From 84bd1fab510f35863f63b8517628b1f928511ef8 Mon Sep 17 00:00:00 2001 From: "a.shamavov" <a.shamavov@iqdev.digital> Date: Mon, 8 Apr 2024 17:03:53 +0500 Subject: [PATCH] add countFriday13 --- src/Controller/HomeController.php | 28 +++++++++++++++++++--------- templates/home.html.twig | 5 ++++- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index c0b1db0..3436029 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -2,6 +2,9 @@ namespace App\Controller; +use DateInterval; +use DatePeriod; +use DateTime; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; @@ -9,17 +12,24 @@ use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController { - private function howDaysToNy(DateTimeImmutable $date): int { - $endYear = date("Y-12-31", date_timestamp_get($date)); - $dateInterval = date_diff(new DateTimeImmutable($endYear), $date); - return $dateInterval->format("%a") + 1; + private function countFriday13(int $year): iterable { + $stardDate = new DateTime("{$year}-01-01 Friday"); + $year += 1; + $endDate = new DateTime("{$year}-01-01"); + $interval = new DateInterval('P7D'); + foreach(new DatePeriod($stardDate, $interval, $endDate) as $day) { + yield new DateTimeImmutable($day->format("Y-m-d")); + } } - #[Route('/', name: 'home')] - public function home(): Response - { - $count = $this->howDaysToNy(new DateTimeImmutable()); - return $this->render('home.html.twig', ['count' => $count]); + #[Route('/{year}', name: 'home')] + public function home(int $year): Response + { + $fridays = array(); + foreach($this->countFriday13($year) as $date) { + $fridays[] = $date->format("Y-m-d l"); + } + return $this->render('home.html.twig', ['fridays' => $fridays]); } } diff --git a/templates/home.html.twig b/templates/home.html.twig index 6ce69c6..bb1d738 100644 --- a/templates/home.html.twig +++ b/templates/home.html.twig @@ -1,3 +1,6 @@ {% block body %} - <h1>Кол-во дней до ÐГ: {{ count }}</h1> + <h1>Кол-во пÑтниц 13 в году: {{ fridays|length }}</h1> + {% for day in fridays %} + <p>{{ day }}</p> + {% endfor %} {% endblock %} \ No newline at end of file -- GitLab