簡體   English   中英

Laravel 獲取 TimeZone 不工作

[英]Laravel get TimeZone not working

我正在嘗試將UTC時區轉換為Singapore Time

    echo $Date->format('Y-m-d H:i:s');
    echo '<br>'; 
    echo with(new Carbon\Carbon($Date->format('Y-m-d H:i:s')))->tz('Asia/Singapore')->format('Y-m-d H:i:s');
    exit;

它在Localhost中工作正常,但在aws server中它顯示utc Time ...服務器和 Localhost 都設置為UTC時間。我們如何解決這個問題?

我假設$Date在您的情況下是DateTime對象(或Carbon對象)。 您可以使用PHP setTimeZone()DateTimeZone

$Date = new DateTime($user->updated_at); // sample DateTime creation - also $Date = $user->updated_at; would work here

echo $Date->format("Y-m-d H:i:s")."<br />";

$Date->setTimezone(new DateTimeZone('Asia/Singapore'));
echo  $Date->format("Y-m-d H:i:s")."<br />";

對我來說,它生成:

2014-09-19 12:06:15
2014-09-19 20:06:15

我假設您擁有的$Date變量是Carbon (默認情況下,在Laravel可用)對象:

$Date->format('Y-m-d H:i:s');

現在設置timezone您可以使用以下任何一種:

$Date->timezone = new DateTimeZone('Asia/Singapore');
$Date->timezone = 'Asia/Singapore';
$Date->tz = 'Asia/Singapore';

您也可以從app/config/app.php例如:

/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/

'timezone' => 'Asia/Singapore',

您可以像下面這樣設置為 session;

 Session::put('timezone', 'Your/TimeZone');

享受你的代碼!

試試這個會起作用

 $timestamp ='1411205843'  //Timestamp which you need to convert 

 $sourceTimezone = new DateTimeZone('UTC');

 $destinationTimezone = new DateTimeZone('Asia/Singapore'); 

 $dt = new DateTime(date('m/d/Y h:i A', $timestamp), $sourceTimezone);
 $dt->setTimeZone($destinationTimezone);

 echo strtotime($dt->format('m/d/Y h:i A'));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM