diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index c0b1db0ccaad12b9675d4b3571f1fc95f910924d..343602955118e1608e11e19e4c7a6ad3bc5ec6fa 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 6ce69c6ff8ec4817cb0d43c5b94b8741627c4edc..bb1d7381066549854b7c126f51b8584f07a82e53 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