2016-06-09 1296 views
5

我有一個基於CET地區的項目。我在config/app.php中設置了CET, 但基數中的所有透視時間戳均以UTC時間存儲?如何爲時間戳設置Laravel Carbon時區?

我該如何爲時間戳設置「全局」時區?

我做了這個測試:

<?php 
$timezone = date_default_timezone_get(); 
echo "The current server timezone is: " . $timezone; 
echo "<br />".date('m/d/Y h:i:s a', time()); 

$mytime = Carbon\Carbon::now(); 
echo "<br />".$mytime->toDateTimeString(); 
?> 

和這裏的結果:

The current server timezone is: CET 
06/09/2016 12:06:04 pm 
2016-06-09 11:06:04 

TNX Ÿ

回答

3

您可以存取器實現它

public function getCreatedAtAttribute($value) 
{ 

return Carbon::createFromTimestamp(strtotime($value)) 
    ->timezone(Config::get('app.timezone')) 
    ->toDateTimeString(); //remove this one if u want to return Carbon object 
} 
9

碳使用默認的DateTime PHP對象,所以使用date_default_timezone_set()功能,例如: date_default_timezone_set('Europe/London');

+0

它被命名爲'date.timezone =澳大利亞/ Melbourne'的php.ini文件中設定。感謝您指點功能。 –

相關問題