Décrivez ici votre problème ou ce que vous cherchez à faire.

Entourez votre code en utilisant "```" pour bien le mettre en forme. (ne copiez pas trop de code)

Hello everyone,
I have a problem in Symfony6 for compare 2 dates.
I have an entity Reservation
Inside i have two dateTime.
The first : createdAt : $createdAt : DateTimeImmutable
The second : date : $date : DateTime

I would like to create a function who compare $createdAt & $date and return a message if $date > $createdAt + 60 Days

I have search on the web and symfony doc, but i'm new and i have some problem to understand how it works. Please i need help for create this function.

Thank's in advance for your answers

1 réponse


Hi,

If you have a DateTimeImmutable, you can modify and return new DateTimeImmutable for comparison without leek the initial date. Is not specific for Symfony. Is simple a PHP with DateTime* classes comparison.

function verifyDateMargin(DateTime $d, DateTimeImmutable $createdAt): bool
{
        $marginAt = $createdAt->modify('+60 days');
        return DateTimeImmutable::createFromMutable($d) > $marginAt;
}

You can save the margin in constant.