2014-09-22 88 views
1

蔭試圖UTC時區轉換爲Singapore TimeLaravel獲得時差不工作

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兩個...服務器和本地主機設置爲UTC時間..我們如何解決這個問題?

+0

我不想無禮但爲什麼你會標記所有的laravel版本?首先你的問題與Laravel無關。可能是因爲Carbon在laravel4的作曲家中默認,但laravel3與你的問題無關。 – 2016-03-03 07:09:58

回答

-1

試試這個它會工作

$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')); 
+1

爲什麼在世界上你會把'DateTime'類與'date()'和'strtotime()'函數混合在一起?這可以簡單地使用'DateTime'類來解決...... ** [DEMO](https://eval.in/197869)** – 2014-09-24 05:57:11

1

我假設$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 
0

我認爲,在$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',