Php Laravel时区不工作
我正在尝试将Php Laravel时区不工作,php,laravel,laravel-4,timezone,laravel-3,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; echo$Date->format('Y-m-dh:
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;
echo$Date->format('Y-m-dh:i:s');
回声“
”;
与(新碳纤维\碳纤维($Date->format('Y-m-dh:i:s'))->tz('Asia/Singapore')->format('Y-m-dh:i:s');
出口
它在
Localhost
中工作正常,但在aws服务器中它同时显示utc时间
。服务器和Localhost都设置为utc
时间。。我们如何解决这个问题呢?我假设在您的例子中,$Date
是DateTime
对象(或Carbon
对象)。您可以使用PHPsetTimeZone()
和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 />";
试试这个,会有用的
$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'));
我假设您拥有的$Date
变量是一个(默认情况下在Laravel
中可用)对象:
现在要设置时区
,您可以使用以下任一选项:
$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',
你到底为什么要把DateTime
类与date()
和strotime()
函数混在一起?只需使用DateTime
类即可简单地解决此问题。。。我不想粗鲁,但你为什么要标记所有的拉威尔版本?首先,你的问题与拉威尔无关。可能是因为laravel4中的composer默认使用碳元素,但laravel3与您的问题无关
$Date->timezone = new DateTimeZone('Asia/Singapore');
$Date->timezone = 'Asia/Singapore';
$Date->tz = 'Asia/Singapore';
/*
|--------------------------------------------------------------------------
| 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',