PHP Fatal error: Uncaught Exception: DateTime::__construct()
-
The plugin was throwing this error:
PHP Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (9 marzo, 2017) at position 5 (z): The timezone could not be found in the database in /httpdocs/wp-content/plugins/cache-control/cache-control.php:191
The page was returning a HTTP 500 Internal server error showing the white screen of death.
At cache-control.php line 191, i have seen:
$date_mod = new DateTime( get_the_modified_date() );
A date object is created from the string retrieved by get_the_modified_date()
At wordpress codex we can read that get_the_modified_date( $d, $post ) returns (string) the date on which the post was last modified. The function parameters are both optional and $d in particular is (string) (optional) Optional PHP date format. Defaults to the “date_format” option.
My wordpress installation is in Spanish and the date_format option is by default j F, Y. The date string retrieved by get_the_modified_date is not valid to create a new DateTime object.
To ensure that the $date_mod DateTime object is correctly created for any possible date_format option value, i have changed the line to this:
$date_mod = new DateTime( get_the_modified_date( 'Y-m-d' ) );
That way we ensure that the value retrived by get_the_modified_date is alwais going to be correct to create the $date_mod DateTime object no matter the date_format option value.
Here is the patch to fix the issue:
Index: trunk/cache-control.php =================================================================== --- trunk/cache-control.php (revision 1645039) +++ trunk/cache-control.php (working copy) @@ -205,7 +205,7 @@ if ( $option_name == 'singles' && get_option( 'cache_control_singles_mmulti' ) == 1 ) { $date_now = new DateTime(); - $date_mod = new DateTime( get_the_modified_date() ); + $date_mod = new DateTime( get_the_modified_date( 'Y-m-d' ) ); $last_com = get_comments( 'post_id=' . get_the_ID() . '&number=1&include_unapproved=1&number=1&orderby=comment_date' ); if ( $last_com != NULL ) {
- The topic ‘PHP Fatal error: Uncaught Exception: DateTime::__construct()’ is closed to new replies.