Getting the wrong timezone when outputting user registration date in Wordpress

Mario

New Member
#1
I'm trying to output user registration time in the local timezone, but Wordpress stores the user_registeredvalue in UTC format. Backend settings are set to be +2h.

The following code:
Mã:
$user_registered = $user_info->user_registered;
will display the UTC time like 2018-10-25 15:04:45, where I need it to be 2018-10-25 17:04:45

I tried to get the local time of user registration with get_gmt_from_date function like this
Mã:
$formatted_time = get_gmt_from_date( $user_registered, $format = 'H:i:s');
but that is not working. How do I output local time format for user registration time?
 

Admin

Administrator
Thành viên BQT
#2
That's good. You want everything stored in UTC.

Just create a DateTime object, then set the appropriate time zone. As an example:

Mã:
<?php

$x = new DateTime('2018-10-25 15:04:45', new DateTimeZone('UTC'));
echo $x->format('Y-m-d H:i:s')."\n";
$x->setTimeZone(new DateTimeZone('Europe/Brussels'));
echo $x->format('Y-m-d H:i:s')."\n";
Which will output:
Mã:
2018-10-25 15:04:45
2018-10-25 17:04:45
See it here: https://3v4l.org/OF8Ff
 
Top