Alice
1
Hello,
I’m trying to get the month out of a date element to compare with another date. I’m using the month() function but it echoes the whole date
if ($event->end()->isNotEmpty()):
echo $event->end()->month();
?>
<br>
<?php endif ?>
the expected result is 06 but it displays the whole date (2025-06-08)
What am I doing wrong ?
Hi Alice, where did you pick up the ->month
field method? It doesn’t exist.
If you use the regular PHP date
handler, you can use ->toDate()
with m
: PHP: DateTimeInterface::format - Manual
$event->end()->toDate('m')
Alice
3
Hello,
Thank you for your answer, it works !
Here is where I found it :
bnomei
4
Just some more context…
$event->end()
is returning a Field ($field | Kirby CMS) which can be transformed using built in Field Methods or custom Field Methods.
The $field->toDate($format)
method lets you format a field storing a date as value into a string.
The field is not a Kirby\Toolkit\Date
object and thus has no $field->month()
method.