我正在尝试将时区转换为UTC
Singapore Time
echo $Date->format('Y-m-d H:i:s');
echo '<br>';
echo with(new CarbonCarbon($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
时间..我们如何解决这个问题??
我假设$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',
试试这个它会起作用
$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'));
您可以像下面这样设置为会话;
Session::put('timezone', 'Your/TimeZone');
享受你的代码!