diff --git a/src/Controller/HowDaysToNYController.php b/src/Controller/HowDaysToNYController.php new file mode 100644 index 0000000000000000000000000000000000000000..2166b8b4dcfe6ed213f80d4b4c7f414a69bb40e6 --- /dev/null +++ b/src/Controller/HowDaysToNYController.php @@ -0,0 +1,30 @@ +<?php + +namespace App\Controller; + +use DateTimeImmutable; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Attribute\Route; +use function App\Repository\howDaysToNy; + +class HowDaysToNYController extends AbstractController +{ + /** + * Контролер раÑÑчитывает кол-во дней до нового года иÑÐ¿Ð¾Ð»ÑŒÐ·ÑƒÑŽÑ howDaysToNy + * @param Request $request + * @return Response + * @throws \Exception + */ + #[Route('/howdaystony', name: 'app_how_days_to_n_y')] + public function index(Request $request): Response + { + $date = $request->toArray()['date']; + + $return = howDaysToNy(new DateTimeImmutable($date, null)); + + return new JsonResponse($return, Response::HTTP_OK); + } +} diff --git a/src/Repository/HowDaysToNYRepository.php b/src/Repository/HowDaysToNYRepository.php new file mode 100644 index 0000000000000000000000000000000000000000..90ab47a20f4aa1c217af96d38eb4c0fd832104bd --- /dev/null +++ b/src/Repository/HowDaysToNYRepository.php @@ -0,0 +1,19 @@ +<?php +namespace App\Repository; + +use DateTime; +use DateTimeImmutable; + +/** + * Ð¤ÑƒÐ½ÐºÑ†Ð¸Ñ Ñ€Ð°ÑÑчитывает кол-во дней до нового года + * @param DateTimeImmutable $date дата от которой, необходимо раÑÑчитать кол-во дней + * @return int + */ +function howDaysToNy(DateTimeImmutable $date): int { + + $nextYear = (int)$date->format('Y') + 1; + $dateOfNY = (new DateTime()) + ->setDate($nextYear, 01, 01); + + return $dateOfNY->diff($date)->format('%a'); +}