Opened 6 years ago
Last modified 5 years 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)
Change History (4)
#1
@
6 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
#2
@
5 years 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)
#3
@
5 years 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.
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?