Skip to content
DiffDaysController.php 959 B
Newer Older
Akex's avatar
Akex committed
<?php

namespace App\Controller;

use DateTimeImmutable;
use HttpResponse;
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;

class DiffDaysController extends AbstractController
{
    /**
     * Контроллер вернет кол-во дней между датами
     * @param Request $request
     * @return Response
     */
    #[Route('/diff/days', name: 'app_diff_days', methods: ['POST'])]
    public function index(Request $request): Response
    {
        $dateStart = new DateTimeImmutable($request->toArray()['date_start']);
        $dateEnd = new DateTimeImmutable($request->toArray()['date_end']);

        return new JsonResponse(["interval"=>($dateStart->diff($dateEnd)->format("%a"))]
            , Response::HTTP_OK);
    }
}