Laravel让TimeZone无法正常工作

时间:2014-09-22 07:55:54

标签: php laravel laravel-4 timezone laravel-3

我尝试将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时间..我们如何修复此??

3 个答案:

答案 0 :(得分:1)

我假设你的$DateDateTime个对象(或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

答案 1 :(得分: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',

答案 2 :(得分:-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'));