Good idea to start a test site 🙂
This:
[types field='session-start' style='text' format='F j, Y, g:i a e'][/types]
Displays:
September 14, 2018, 12:00 am Europe/Amsterdam
Midnight display is correct because it was a date field without a time option.
However, the issue is the database entry. I updated enlace oculto to have this content:
Text: [types field='session-start' style='text' format='F j, Y, g:i a e'][/types]
Timestamp: [types field='session-start' style='text' format='U'][/types]
This displays:
Putting that into enlace oculto confirms it is midnight UTC (your server's time zone), not midnight Amsterdam (WP's time zone).
Technical points:
- using PHP's date() will use the server's time zone, which is usually (should be) UTC, but it could be different, and it is open to manipulation by poorly-coded plugins that may utilize date_default_timezone_set() (poorly coded if they don't cleanup after themselves or affect your plugin unintentionally)
- because the WP time zone can change at any time, saving all the timestamps in the database in the time zone from WP's settings is ripe for potential trouble
- therefore the field itself should have a time zone option/field, just like date has optional "time" option/field. The time zone field should not be optional because defaulting to midnight is still time zone dependent.
- make sure the time zone selection list defaults to WP's chosen time zone if it's a valid one from timezone_identifiers_list() (see enlace oculto, enlace oculto">code and article written by me)
Alternative solutions:
- PHP DateTime class is the best but only works with valid time zones, and WP pollutes with manual UTC offsets (which, IMO, are only ever chosen by users out of confusion and WP should remove these)
- or you could use strtotime() but you wouldn't do
strtotime( '2018-09-14' )
, instead you would also pass the time zone string:
strtotime( '2018-09-14 Europe/Amsterdam' )
and that would be an accurate timestamp saved to the database (could even support WP's manual UTC offset time zones but this is terrible)
- Even better, instead of saving timestamps, save as a serialized array containing the date, time, and time zone:
['date' => '2018-09-14', 'time' => '17:30:00', 'time_zone' => 'Europe/Amsterdam' ]
- Doing it this way will have many benefits: could build multi-time zone apps (calendar) and support people outside of the site's own time zone (site is in Chicago, users are across all of the USA); human-readable database entries; your shortcode/function could add a time zone parameter, which would allow for converting from the saved time to any other valid time zone (if not set, would use the field's time zone value, of course)
I'm happy to help more if needed. My goal is that your product is made better by handling "time" correctly (fixing the bug) and hopefully also making it more robust (features).