I need to display event CPTs limited by date (today) and time (event is now running).
To do so, I’m using a meta query comparing
- a date picker field to date(‘Y-m-d’)
- a start time picker field to date(‘H:i:s’)
- an end time picker field to date(‘H:i:s’)
To be able to use local server time correctly, I need to add date_default_timezone_set(“Europe/Berlin”) before doing php date / time camparisons.
However, as soon as this is added, all ACF time / date fields start displaying and calculating as UTC time, i.e. a meta entry of 12:15:00 in the database is displayed as 11:15:00 using ACF template functions.
Even doing ridiculous stuff like
date_default_timezone_set("Europe/Berlin");
$starttime = date('H:i:s', strtotime( get_field( 'event_starttime' ) ) );
doesn’t seem to help.
Is there an easy way to solve this without having to dive into (to me) incomprehensible and overcomplicated (again, to me) DateTimeImmutable or datefmt_format stuff?
You cannot use date_default_timezone_set() when working with WP. There was a change in WP 5.3 that broke the use of this function.
See https://docs.wpvip.com/technical-references/code-quality-and-best-practices/local-time/
Ahhh, that explains a lot of weird stuff that had been going on.
Thanks a lot for pointing it out, completely missed this!
(It might be useful if this was somehow even passingly mentioned here, IMHO)