Make WordPress Core

Opened 2 years ago

Last modified 4 weeks ago

#56358 assigned defect (bug)

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

Reported by: toastercookie's profile toastercookie Owned by: hellofromtonya's profile hellofromTonya
Milestone: 6.7 Priority: normal
Severity: minor Version: 6.0
Component: Date/Time Keywords: php80
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 (10)

#1 @Rarst
2 years 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 2 years ago by Rarst (previous) (diff)

#2 in reply to: ↑ description @SergeyBiryukov
2 years 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
2 years 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
2 years ago

  • Keywords php8 added
  • Milestone Awaiting Review deleted

No worries, thanks for the follow-up!

#6 @verygoode
5 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 5 months ago by verygoode (previous) (diff)

#7 @verygoode
5 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 5 months ago by verygoode (previous) (diff)

#8 @verygoode
5 months ago

  • Focuses php-compatibility added

#9 @verygoode
5 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 5 months ago by verygoode (previous) (diff)

#10 @hellofromTonya
4 weeks ago

  • Keywords php80 added; php8 removed
  • Milestone set to 6.7
  • Owner set to hellofromTonya
  • Status changed from reopened to assigned

Related: #57035, #58986, #60629.

Each of these tickets is dealing with the same issue, which I summarized in [57035#comment:39]:

I'm thinking this area needs a deeper contextual dive as well as consideration and testing to raise confidence of no side effects or regressions being introduced. It'll also need more test coverage (happy and unhappy paths).

The challenges:

  • robust solution to handle all timezones include partial hour timezones.

    The gmt_offset can be a float/double for partial-hour timezones such as +5:30 UTC for India and Sri Lanka.

  • review of what values gmt_offset should and could have.
  • and cross-version compatibility for the PHP versions WP supports.

I'm also curious of how this is solved in other projects.

Pulling it into 6.7 and self-assigning with intent to help shepherd the effort for these tickets.

Note: See TracTickets for help on using tickets.