diff --git a/src/Action/Functions.php b/src/Action/Functions.php index acd4ab97f7e62667f7033b8dee64d148873ed0fc..e1625b17f3756cdf36607481814d3cd1ff8c619e 100644 --- a/src/Action/Functions.php +++ b/src/Action/Functions.php @@ -120,4 +120,16 @@ class Functions yield new DateTimeImmutable($day->format("Y-m-d")); } } + + /** + * Вернет кол-во дней между датами + * @param DateTimeImmutable $dateStart дата начала + * @param DateTimeImmutable $dateEnd дата окончания + * @return int + */ + public function diffDays(DateTimeImmutable $dateStart, DateTimeImmutable $dateEnd): int + { + $dateInterval = date_diff($dateStart, $dateEnd); + return (int)$dateInterval->format("%a") ; + } } \ No newline at end of file diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 1d3494287cd0a17a857c5173ca974c81ec013348..3396331167525f8f0ac06943cdb69ee833bbc1e1 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -9,7 +9,8 @@ use App\Requests\{ UniqElementsRequest, MenuRequest, HowDaysToNyRequest, - CountFriday13Request + CountFriday13Request, + DiffDaysRequest }; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -76,4 +77,18 @@ class HomeController extends AbstractController } return $this->json($fridays); } + + #[Route('/diffDays', name: 'diffDays')] // 01-01-2024 + public function diffDays(DiffDaysRequest $request): Response + { + try { + $result = $this->functions->diffDays( + new DateTimeImmutable($request->getRequest()->get('startDate')), + new DateTimeImmutable($request->getRequest()->get('endDate')), + ); + return $this->json(["The difference of days:" => $result]); + } catch (\Exception $e) { + return new Response($e->getMessage()); + } + } } diff --git a/src/Requests/DiffDaysRequest.php b/src/Requests/DiffDaysRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..8cba54ec46355d1a907b4ae71183f235dc6e3959 --- /dev/null +++ b/src/Requests/DiffDaysRequest.php @@ -0,0 +1,14 @@ +Кол-во пятниц 13 в году: {{ fridays|length }} - {% for day in fridays %} -

{{ day }}

- {% endfor %} +

Кол-во дней между датами: {{ count }}

{% endblock %} \ No newline at end of file