I’m not sure how string comparisons work in PHP, but it might be more reliable to compare timestamps (numbers).
One thing that may be happening is that ->toDate('Y-m-d')
does two conversions:
- first it needs to parse the field’s value (a string) into a timestamp (a number);
- then it needs to format that number back into a string.
The string '2020-06-01'
converts to a timestamp that represents the June 1st at midnight (the very start of the day). I wouldn’t be surprised if when converting back to a date, it would take the server’s timezone into account and convert the date to '2020-05-31'
.
If you do the work yourself and with numbers, it’s a bit easier to control what’s happening (though dealing with dates is always difficult anyway because humans are weird).
Also it might be useful to move that date comparison code in its own function, maybe in a page model? If the template name for your sub-pages is ‘subpage’, it could look like:
<?php // site/modes/subpage.php
class SubpagePage extends Page {
public function isVisibleToday(): bool {
$from = $this->content()->dateVon()->toDate();
$to = $this->content()->dateBis()->toDate();
if (is_int($from) && is_int($to)) {
$now = time();
// Provided the 'to' date is written as 'YYY-MM-DD' with no time,
// the time will be set to midnight (start of the day), but we want
// the content to be visible up to midnight at the end of the day.
return $now >= $from && $now < ($to + 24 * 60 * 60);
}
return false;
}
}
Note that we have to add a full day (as a number of seconds) to the ‘to’ date, to make sure that time actually means “that day, but at the very end of the day”.
Another option is to declare your dates with a time as well, so that you can control more precisely when a subpage is visible or not.
So your content could look like
----
datevon: 2020-05-31 00:00
----
datebis: 2020-06-01 00:00
----
or even:
----
datevon: 2020-05-31 06:00
----
datebis: 2020-05-31 22:00
----
(If you end up using explicit times in your dates, you should change the ($to + 24 * 60 * 60)
to just $to
in the example above.)
See also: Cookbook - Working with dates.
Quick warning about timezones
I think Kirby parses dates with the strtotime PHP function. I’m not sure if the times (including the default “midnight”) will match a specific timezone.
If you want “2020-05-31 06:00” (or “2020-05-31” which is implicitly “2020-05-31 00:00”) to mean “6 o’clock in the timezone that I care about, e.g. the timezone for Germany”, you should check that the web server and your local PHP version use the same timezone:
var_dump(date_default_timezone_get());
// e.g. "Europe/Berlin"
otherwise, e.g. if it returns “UTC”, you can end up in situations where your content end up being visible the day before at 22:00 instead of midnight.
You can probably add a line like this in your config.php to set the timezone you want:
date_default_timezone_set('Europe/Berlin');