WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 9 months ago

#45047 new defect (bug)

user_registered returned as UTC Time instead of Local Time

Reported by: kcrnc Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Date/Time Keywords: has-patch
Focuses: multisite Cc:

Description

I apologize if this has been discussed/covered before or I am missing something obvious, but I did a few searches, both through the code base and also bug-tracker/google, and couldn't find anything on this subject.

While trying to debug a timezone issue for a client, I noticed that user_registered appears to be set to GMT/UTC time by default, rather than the local time set via settings.

I searched the code and found where I think this is set:

wp-includes/user.php

<?php
        $user_registered = empty( $userdata['user_registered'] ) ? gmdate( 'Y-m-d H:i:s' ) : $userdata['user_registered'];

I also tested adding a user on a relatively clean site via the default add user option and verified it gets set to UTC, regardless of the local time setting in wordpress.

So, when you grab the user via get_userdata(), the $user_data->user_registered field looks like it will always be UTC time, unless presumably the user is created manually(wp_create_user or equivalent) and you set the time during creation.

I think setting it in ALL cases to UTC probably makes sense, as you never know when a person is going to change their timezone settings in wordpress, the settings on their server, etc. And, timezones can be a pain to work with in general, so having it be UTC by default makes sense.

However, when returning it back to the end-user, it would be useful to have it take into account their local timezone setting in WordPress, at least by maybe adding a $user_data->user_registered_local field to the user object.

Attachments (1)

45047.patch (1.3 KB) - added by ocean90 9 months ago.

Download all attachments as: .zip

Change History (4)

#1 @SergeyBiryukov
2 years ago

  • Summary changed from user_registered returned as UTC Timeinstead of Local Time to user_registered returned as UTC Time instead of Local Time

@ocean90
9 months ago

#2 @ocean90
9 months ago

  • Component changed from Users to Date/Time
  • Focuses multisite added
  • Keywords has-patch added
  • Milestone changed from Awaiting Review to Future Release
  • Type changed from enhancement to defect (bug)

Hello @kcrnc, thanks for the report.

I just noticed the same and it also applies to the date values visible in the users/sites list table in multisite.
While we can't change the way how the dates are saved (see also #48097) we can fix the date display in the UI, see 45047.patch.

@Rarst any thoughts on the suggested patch?

#3 @Rarst
9 months ago

I would prefer to drop mysql2date() there (and everywhere), it's very problematic. It expects a local time and produces a WP timestamp. Using it for UTC time is a case of compounding bugs ending up in "correct" result more by accident than design.

Possibly replace with date_create_from_format() and explicit UTC time zone.

Note: See TracTickets for help on using tickets.