Newer
Older
use DateInterval;
use DatePeriod;
use DateTime;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class HomeController extends AbstractController
{
private function countFriday13(int $year): iterable {
$stardDate = new DateTime("{$year}-01-01 Friday");
$year += 1;
$endDate = new DateTime("{$year}-01-01");
$interval = new DateInterval('P7D');
foreach(new DatePeriod($stardDate, $interval, $endDate) as $day) {
yield new DateTimeImmutable($day->format("Y-m-d"));
#[Route('/{year}', name: 'home')]
public function home(int $year): Response
$fridays = array();
foreach($this->countFriday13($year) as $date) {
$fridays[] = $date->format("Y-m-d l");
return $this->render('home.html.twig', ['fridays' => $fridays]);
#[Route('/{startDate}/{endDate}', name: 'home')] // 01-01-2024
public function home(string $startDate, string $endDate): Response
if (DateValidation::validate($startDate) && DateValidation::validate($endDate)) {
try {
$result = $this->functions->diffDays(
new DateTimeImmutable($startDate),
new DateTimeImmutable($endDate)
);
return $this->json(["The difference of days:" => $result]);
} catch (\Exception $e) {
return new Response($e->getMessage());
}
}
return new Response("Invalid date format");