2017-01-16 116 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