2017-01-16 121 views
0

PHP的地方时我使用这个功能来显示WP页面(通过function.php)的小时显示在WP内容页

function displaydate(){ 
return date('H:i'); 
} 

但retourns我的服务器小时。

我希望它向我展示欧洲/巴黎时间。

感谢

回答

1

使用date_default_timezone_set设置时区:

function displaydate(){ 
    date_default_timezone_set('Europe/Paris'); 
    return date('H:i'); 
} 
1

我建议使用DateTime类来替代date()一旦你开始添加复杂性,例如更改时区。

实施例:

function displaydate() { 
    $datetime = new DateTime; 
    $paris_tz = new DateTimeZone('Europe/Paris'); 

    $datetime->setTimezone($paris_tz); 

    return $datetime->format('H:i'); 
} 

进一步阅读:http://php.net/manual/en/class.datetime.php