From 36458228f2cfa349a294b4895a125e854f7b0e06 Mon Sep 17 00:00:00 2001 From: "a.shamavov" <a.shamavov@iqdev.digital> Date: Mon, 8 Apr 2024 17:10:45 +0500 Subject: [PATCH] add diffDays --- src/Controller/HomeController.php | 22 ++++++---------------- templates/home.html.twig | 5 +---- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 3436029..adf7c3d 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -12,24 +12,14 @@ use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController { - 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")); - } + private function diffDays(DateTimeImmutable $dateStart, DateTimeImmutable $dateEnd): int { + return date_diff($dateStart, $dateEnd)->format("%a") ; } - - #[Route('/{year}', name: 'home')] - public function home(int $year): Response + #[Route('/{startDate}/{endDate}', name: 'home')] // 01-01-2024 + public function home(string $startDate, string $endDate): Response { - $fridays = array(); - foreach($this->countFriday13($year) as $date) { - $fridays[] = $date->format("Y-m-d l"); - } - return $this->render('home.html.twig', ['fridays' => $fridays]); + $countDays = $this->diffDays(new DateTimeImmutable($startDate), new DateTimeImmutable($endDate)); + return $this->render('home.html.twig', ['count' => $countDays]); } } diff --git a/templates/home.html.twig b/templates/home.html.twig index bb1d738..3814758 100644 --- a/templates/home.html.twig +++ b/templates/home.html.twig @@ -1,6 +1,3 @@ {% block body %} - <h1>Кол-во пÑтниц 13 в году: {{ fridays|length }}</h1> - {% for day in fridays %} - <p>{{ day }}</p> - {% endfor %} + <h1>Кол-во дней между датами: {{ count }}</h1> {% endblock %} \ No newline at end of file -- GitLab