Skip to content
Snippets Groups Projects
Commit e94777b5 authored by Akex's avatar Akex Committed by Александр Плохих
Browse files

make controller

parent 4ebf14ce
No related branches found
No related tags found
1 merge request!5make controller
<?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);
}
}
<?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');
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment