2014-09-22 30 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',