Make WordPress Core

Opened 22 months ago

Last modified 3 months ago

#56358 reopened defect (bug)

PHP8 TypeError on current_time( 'timestamp' ) if timezone is set to GMT / 0

Reported by: toastercookie's profile toastercookie Owned by:
Milestone: Priority: normal
Severity: minor Version: 6.0
Component: Date/Time Keywords: php8
Focuses: php-compatibility Cc:

Description

Just a minor bug to report, seems that if you have your timezone set to GMT, current_time( 'timestamp' ) throws a fatal error on PHP8, I think because 0 is being sent as a string. I can fix by changing my timezone to anything else.

PHP Fatal error: Uncaught TypeError: Unsupported operand types: string * int in /wp-includes/functions.php:75
Stack trace: #0 /RSSItemPull.php(286): current_time('timestamp')

Change History (9)

#1 @Rarst
22 months ago

Do you mean UTC? GMT is currently not an option in WordPress time zone settings and I can't reproduce it on PHP 8.1.

I do not see how this error could happen with current version of the current_time() code. Which WordPress version are you on?

I do see that option could return "0" there (for UTC+0 time zone). However, from quick check, multiplying that by an integer is a valid operation in PHP 8, it would type cast it to a number. https://3v4l.org/sGXiQ

Last edited 22 months ago by Rarst (previous) (diff)

#2 in reply to: ↑ description @SergeyBiryukov
22 months ago

Hi there, welcome to WordPress Trac! Thanks for the ticket.

Replying to toastercookie:

Just a minor bug to report, seems that if you have your timezone set to GMT, current_time( 'timestamp' ) throws a fatal error on PHP8, I think because 0 is being sent as a string. I can fix by changing my timezone to anything else.

I could not reproduce the error in my testing. As noted above, '0' should not be an issue, as it is still a numeric string.
Could you look into what get_option( 'gmt_offset' ) returns, or what is the value in the wp_options table?

I can only reproduce this with a non-numeric string like Europe/London, but that should be in the timezone_string option, not in gmt_offset. Does the issue still happen on a clean install, with all plugins disabled and a default theme (Twenty Twenty-Two) activated?

Replying to Rarst:

Do you mean UTC? GMT is currently not an option in WordPress time zone settings and I can't reproduce it on PHP 8.1.

It looks like the English (UK) locale translates UTC as GMT.

#3 @toastercookie
22 months ago

  • Resolution set to invalid
  • Status changed from new to closed

Changing the timezone and then changing it back seems to have fixed it for me, so there must have been something weird in my DB being returned. I can't reproduce this on my end anymore so I will just go ahead and close this! Sorry for wasting yall's time

#4 @SergeyBiryukov
22 months ago

  • Keywords php8 added
  • Milestone Awaiting Review deleted

No worries, thanks for the follow-up!

#6 @verygoode
3 months ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

Encountered on a site, found that in some situations the gmt_offset option may get left empty -- seemingly due to plugin conflicts causing the save function to error out before gmt_offset gets stored.

Here are two example set of reproduction instructions using WordPress 6.4.3 and PHP 8.1.

Example 1

  1. Access /wp-admin/options-general.php and set a timezone with an offset like UTC+1
  2. Edit gmt_offset option in DB, set to empty
  3. Observe /wp-admin/options-general.php's Date Format option is now experiencing a critical error with the following fatal logged. Cannot save page via UI.
PHP Fatal error:  Uncaught TypeError: Unsupported operand types: string * int in /wordpress/core/6.4.3/wp-includes/functions.php:76
Stack trace:
#0 /wordpress/core/6.4.3/wp-includes/functions.php(180): current_time('timestamp', false)
#1 /wordpress/core/6.4.3/wp-admin/options-general.php(364): date_i18n('F j, Y')
#2 {main}
  thrown in /wordpress/core/6.4.3/wp-includes/functions.php on line 76

Example 2

This example demonstrates how a plugin might interfere with gmt_offset being set.

  1. Install and activate aryo-activity-log version 2.9.1
  2. Navigate to /wp-admin/options-general.php and change Timezone to UTC and Start of Week to Sunday
  3. Encounter the following fatal
PHP Fatal error:  Uncaught TypeError: Unsupported operand types: string * int in /wordpress/core/6.4.3/wp-includes/functions.php:76
Stack trace:
#0 /srv/htdocs/wp-content/plugins/aryo-activity-log/classes/class-aal-api.php(95): current_time('timestamp')
#1 /srv/htdocs/wp-content/plugins/aryo-activity-log/classes/class-aal-api.php(186): AAL_API->insert(Array)
#2 /srv/htdocs/wp-content/plugins/aryo-activity-log/hooks/class-aal-hook-options.php(105): aal_insert_log(Array)
#3 /srv/htdocs/wp-content/plugins/aryo-activity-log/hooks/class-aal-hook-options.php(96): AAL_Hook_Options->insert_log('start_of_week')
#4 /wordpress/core/6.4.3/wp-includes/class-wp-hook.php(324): AAL_Hook_Options->hooks_updated_option('start_of_week', '1', 0)
#5 /wordpress/core/6.4.3/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#6 /wordpress/core/6.4.3/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#7 /wordpress/core/6.4.3/wp-includes/option.php(894): do_action('updated_option', 'start_of_week', '1', 0)
#8 /wordpress/core/6.4.3/wp-admin/options.php(339): update_option('start_of_week', 0)
#9 {main}
  thrown in /wordpress/core/6.4.3/wp-includes/functions.php on line 76
  1. Observe that the gmt_offset option is empty in the database.
  2. Revisit /wp-admin/options-general.php, observe critical error message under Date Format option, lack of Save button. Observe the following fatal.
PHP Fatal error:  Uncaught TypeError: Unsupported operand types: string * int in /wordpress/core/6.4.3/wp-includes/functions.php:76
Stack trace:
#0 /wordpress/core/6.4.3/wp-includes/functions.php(180): current_time('timestamp', false)
#1 /wordpress/core/6.4.3/wp-admin/options-general.php(364): date_i18n('F j, Y')
#2 {main}
  thrown in /wordpress/core/6.4.3/wp-includes/functions.php on line 76

Resolving

To resolve, give the gmt_offset option a value.

E.g., via WP-CLI: wp option update gmt_offset 0

Last edited 3 months ago by verygoode (previous) (diff)

#7 @verygoode
3 months ago

With https://core.trac.wordpress.org/ticket/56358#comment:6, it seems current_time might need to be a bit more robust to avoid fatals when the gmt_offset option is empty.

Otherwise, the user is left with a broken /wp-admin/options-general.php and cannot adjust the Date Format option and can't save the page.

Last edited 3 months ago by verygoode (previous) (diff)

#8 @verygoode
3 months ago

  • Focuses php-compatibility added

#9 @verygoode
3 months ago

Similar issue with patch discussion, https://core.trac.wordpress.org/ticket/58986

And another similar report, https://core.trac.wordpress.org/ticket/60629

Last edited 3 months ago by verygoode (previous) (diff)
Note: See TracTickets for help on using tickets.