DateTime::createFromFormat(): Failed to parse time string in modx/date.php
0 reputation · 19 Jun 2023, 19:48 UTC
0 reputation · 19 Jun 2023, 19:48 UTC
To determine how ModX should handle regional time zones when converting stored UTC timestamps to the user’s locale.
The core file modx/model/modx/date.php throws a generic exception when DateTime::createFromFormat receives an invalid timezone string, resulting in the PHP warning "DateTime::createFromFormat(): Failed to parse time string". ModX stores all timestamps as UTC in the database, but the PHP DateTime objects are instantiated using the server’s default timezone unless overridden by the user’s locale settings. The time_zone system setting only sets the default PHP timezone during bootstrap and does not affect how dates are displayed for users in different regions.
There is no automatic conversion of stored UTC timestamps to the user’s timezone when rendering dates in the front‑end or manager interface, and error messages for unsupported timezone identifiers are not localized.
The DateTime::createFromFormat() warning appears when the timezone token in your format string is given a value PHP cannot parse. In standard MODX, the core does not automatically convert stored UTC timestamps to each user's local timezone in the manager or front-end. It applies a single global timezone from the time_zone system setting during bootstrap.
Addressing your specific questions:
DateTime object in UTC and then call setTimezone() with the target user's zone.timezone_identifiers_list() before formatting, or catch the Exception thrown by new DateTimeZone(). Route the failure through MODX's error handler or a lexicon entry rather than exposing the raw PHP warning.Confirmed: DateTime::createFromFormat() returns false and emits a warning when the input does not match the format, including an unrecognized timezone identifier. Separately, the DateTimeZone constructor throws an Exception for invalid strings, which is a distinct failure mode.
Likely: The value reaching modx/model/modx/date.php is empty, misspelled, or a non-IANA abbreviation while the format expects a valid identifier such as Europe/London.
if (!in_array($tz, timezone_identifiers_list())) { /* handle error */ }$date = new DateTime('2026-10-15 12:00:00', new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone($tz));
echo $date->format('Y-m-d H:i:s');
createFromFormat(), check its return value and fall back to a lexicon-backed error message instead of letting the warning propagate.Reproduce the warning with a minimal script: DateTime::createFromFormat('Y-m-d H:i:s e', '2026-10-15 12:00:00 UTC-Wrong'). Compare date_default_timezone_get() with your MODX time_zone setting. Then run the UTC-to-target conversion above and confirm the offset matches the target zone.
This assumes an unmodified MODX core and no extra that overrides the date processor. If a third-party extra rewrites date.php behavior, the patch location may differ.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.