Commit 634b261b authored by Nikita Chernykh's avatar Nikita Chernykh
Browse files

Merge branch 'PTPS_Controller_7' into 'main'

Ptps controller 7

See merge request !7
parents ca9bf67d 9e00902d
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -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
+16 −1
Original line number Diff line number Diff line
@@ -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());
        }
    }
}
+14 −0
Original line number Diff line number Diff line
<?php

namespace App\Requests;

use Symfony\Component\Validator\Constraints as Assert;

class DiffDaysRequest extends BaseRequest
{
    #[Assert\Type('date')]
    public $startDate;

    #[Assert\Type('date')]
    public $endDate;
}
 No newline at end of file
+1 −4
Original line number Diff line number Diff line
{% 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