#[Route('/show/{id}', name: 'app_warning_show', methods: ['GET'])]
public function show(Warning $warning): Response
{
$time = new DateTime('now');
$this->checkAccess(['ROLE_WHITELIST','ROLE_FRACTION','ROLE_SUPPORT','ROLE_ADMIN']);
return $this->render('warning/show.html.twig', [
'warning' => $warning,
'expired' => $time > $warning->getEnd(),
]);
}
Created Datetime in DB -> 2024-04-06 12:13:00
Duration are 10h
public function getEnd() {
return date('d.m.Y - H:i:s', ($this->getCreated()->getTimestamp() + ($this->getFrakBanDuration() * 3600)));
}
Try 1: (getCreated()
) 2024-04-06 12:13:00 + (getFrakBanDuration()
) 10h = (getEnd()
) 06.04.2024 – 22:13:00
Try 2: (getCreated()
) 2024-04-06 12:13:00 + (getFrakBanDuration()
) 23h = (getEnd()
) 07.04.2024 – 11:13:00
Try 3: (getCreated()
) 2024-04-06 12:13:00 + (getFrakBanDuration()
) 100h = (getEnd()
) 10.04.2024 – 16:13:00
$time = new DateTime('now');
$time > $warning->getEnd()
IT IS ALWAYS TRUE?????
The condition 'expired' => $time > $warning->getEnd(),
ist ALWAYS True.
I compared the Timestamp and the Object in all directions and options and chanced the duration from 10 to 100. So the new End ist 10.04.2024 the old was 06.04.2024. How the f*** is it possible?? I dont get it.
2
Answers
Verdammt du hast recht. Es war sehr spät gestern und ich habe es nicht mehr verstanden. Macht natürlich sinn das ich keine
strings
miteinander vergleichen kann.Nachdem ich deine Beispiele gesehen habe, habe ich es verstanden. Ich muss nicht die Zeiten las Text sondern als
timestamp
vergleichen, alsoint
vs.int
. Und siehe da es ging.Weiter schwierig war es, da ich es in TWIG vergleichen musste. Und danke für das verkürzen des Codes.
Lösung:
so
1234
>4321
I'm sorry, my fault..
Damn you're right. It was very late yesterday and I didn't understand it anymore. Of course it makes sense that I can't compare
strings
with each other.After seeing your examples, I understood it. I don't have to compare the times as text but as a
timestamp
, i.e.int
vs.int
.It was further difficult because I had to compare it in TWIG.
Solution:
Thanks for shortening the code.
Your code can be summarised as:
which indeed produces
bool(true)
. This is documented at Comparison with Various Types:It does not make sense to use text for date calculations, more than it does for numbers:
If you are using date objects, stick to that. There’s no need to convert to text or Unix time, nor to build your own date calculation logic:
(Note I’m cloning to
$this->getCreated()
to avoid side effects, this is not really part of the calculations).