admin管理员组文章数量:1130349
I need to use PHP's DateTime(). It's currently displaying GMT time instead of the time that's set for the timezone setting (EST) in the WordPress Admin.
How can this be converted to show the the time base
$time = new DateTime();
echo $time->format("Y-m-d h:i:s");
// 2015-08-12 04:35:34 (gmt)
// 2015-08-12 12:35:34 (what i want -- EST)
I need to use PHP's DateTime(). It's currently displaying GMT time instead of the time that's set for the timezone setting (EST) in the WordPress Admin.
How can this be converted to show the the time base
$time = new DateTime();
echo $time->format("Y-m-d h:i:s");
// 2015-08-12 04:35:34 (gmt)
// 2015-08-12 12:35:34 (what i want -- EST)
Share
Improve this question
edited Aug 14, 2015 at 4:58
fuxia♦
107k39 gold badges255 silver badges461 bronze badges
asked Aug 12, 2015 at 16:35
user1462user1462
1,3043 gold badges17 silver badges23 bronze badges
3 Answers
Reset to default 8I'm not sure why EliasNS' answer is marked correct, as far as I'm aware (and from the documentation), the second parameter of DateTime::__construct(), if provided, should be a DateTimeZone instance.
The problem then becomes, how we do we create a DateTimeZone instance. This is easy if the user has selected a city as their timezone, but we can work around it if they have set an offset by using the (deprecated) Etc/GMT timezones.
/**
* Returns the blog timezone
*
* Gets timezone settings from the db. If a timezone identifier is used just turns
* it into a DateTimeZone. If an offset is used, it tries to find a suitable timezone.
* If all else fails it uses UTC.
*
* @return DateTimeZone The blog timezone
*/
function wpse198435_get_blog_timezone() {
$tzstring = get_option( 'timezone_string' );
$offset = get_option( 'gmt_offset' );
//Manual offset...
//@see http://us.php/manual/en/timezones.others.php
//@see https://bugs.php/bug.php?id=45543
//@see https://bugs.php/bug.php?id=45528
//IANA timezone database that provides PHP's timezone support uses POSIX (i.e. reversed) style signs
if( empty( $tzstring ) && 0 != $offset && floor( $offset ) == $offset ){
$offset_st = $offset > 0 ? "-$offset" : '+'.absint( $offset );
$tzstring = 'Etc/GMT'.$offset_st;
}
//Issue with the timezone selected, set to 'UTC'
if( empty( $tzstring ) ){
$tzstring = 'UTC';
}
$timezone = new DateTimeZone( $tzstring );
return $timezone;
}
Then you can use it as follows:
$time = new DateTime( null, wpse198435_get_blog_timezone() );
I had developed a method in my lib to retrieve current WP’s time zone as proper object: WpDateTimeZone::getWpTimezone().
While timezone_string is straightforward (it is already a valid time zone name), gmt_offset case is nasty. Best I could come up with is converting it into a +00:00 format:
$offset = get_option( 'gmt_offset' );
$hours = (int) $offset;
$minutes = ( $offset - floor( $offset ) ) * 60;
$offset = sprintf( '%+03d:%02d', $hours, $minutes )
DateTime aceppts a DateTimeZone parameter, that you can get from WordPress options. Something like:
$time = new DateTime(NULL, get_option('gmt_offset'));
- DateTime
- WordPress TimeZone
Hope this helps.
Edit: I'm testing it and I get an error. I don't know how to convert the timezone string to a DateTimeZone object, but that is the way.
I need to use PHP's DateTime(). It's currently displaying GMT time instead of the time that's set for the timezone setting (EST) in the WordPress Admin.
How can this be converted to show the the time base
$time = new DateTime();
echo $time->format("Y-m-d h:i:s");
// 2015-08-12 04:35:34 (gmt)
// 2015-08-12 12:35:34 (what i want -- EST)
I need to use PHP's DateTime(). It's currently displaying GMT time instead of the time that's set for the timezone setting (EST) in the WordPress Admin.
How can this be converted to show the the time base
$time = new DateTime();
echo $time->format("Y-m-d h:i:s");
// 2015-08-12 04:35:34 (gmt)
// 2015-08-12 12:35:34 (what i want -- EST)
Share
Improve this question
edited Aug 14, 2015 at 4:58
fuxia♦
107k39 gold badges255 silver badges461 bronze badges
asked Aug 12, 2015 at 16:35
user1462user1462
1,3043 gold badges17 silver badges23 bronze badges
3 Answers
Reset to default 8I'm not sure why EliasNS' answer is marked correct, as far as I'm aware (and from the documentation), the second parameter of DateTime::__construct(), if provided, should be a DateTimeZone instance.
The problem then becomes, how we do we create a DateTimeZone instance. This is easy if the user has selected a city as their timezone, but we can work around it if they have set an offset by using the (deprecated) Etc/GMT timezones.
/**
* Returns the blog timezone
*
* Gets timezone settings from the db. If a timezone identifier is used just turns
* it into a DateTimeZone. If an offset is used, it tries to find a suitable timezone.
* If all else fails it uses UTC.
*
* @return DateTimeZone The blog timezone
*/
function wpse198435_get_blog_timezone() {
$tzstring = get_option( 'timezone_string' );
$offset = get_option( 'gmt_offset' );
//Manual offset...
//@see http://us.php/manual/en/timezones.others.php
//@see https://bugs.php/bug.php?id=45543
//@see https://bugs.php/bug.php?id=45528
//IANA timezone database that provides PHP's timezone support uses POSIX (i.e. reversed) style signs
if( empty( $tzstring ) && 0 != $offset && floor( $offset ) == $offset ){
$offset_st = $offset > 0 ? "-$offset" : '+'.absint( $offset );
$tzstring = 'Etc/GMT'.$offset_st;
}
//Issue with the timezone selected, set to 'UTC'
if( empty( $tzstring ) ){
$tzstring = 'UTC';
}
$timezone = new DateTimeZone( $tzstring );
return $timezone;
}
Then you can use it as follows:
$time = new DateTime( null, wpse198435_get_blog_timezone() );
I had developed a method in my lib to retrieve current WP’s time zone as proper object: WpDateTimeZone::getWpTimezone().
While timezone_string is straightforward (it is already a valid time zone name), gmt_offset case is nasty. Best I could come up with is converting it into a +00:00 format:
$offset = get_option( 'gmt_offset' );
$hours = (int) $offset;
$minutes = ( $offset - floor( $offset ) ) * 60;
$offset = sprintf( '%+03d:%02d', $hours, $minutes )
DateTime aceppts a DateTimeZone parameter, that you can get from WordPress options. Something like:
$time = new DateTime(NULL, get_option('gmt_offset'));
- DateTime
- WordPress TimeZone
Hope this helps.
Edit: I'm testing it and I get an error. I don't know how to convert the timezone string to a DateTimeZone object, but that is the way.
本文标签: How to convert DateTime() to display time based on WordPress timezone setting
版权声明:本文标题:How to convert DateTime() to display time based on WordPress timezone setting? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749237631a2337542.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论