问题
I have a problem when I get datetime in my yii2 project. When I get datetime, the date is true but the time is wrong. I execute my code and the result is :
2016-05-02 12:30:28
whereas the time in my laptop is : 19:30. What's the problem? I use time in Indonesia. This is my code:
$time = new \DateTime('now', new \DateTimeZone('UTC'));
$model->tanggal_sampai = $time->format('Y-m-d H:i:s');
回答1:
You are asking for the time in the UTC time zone with new \DateTimeZone('UTC'). Either ask for it in your own time zone, which I think is WIB:
$time = new \DateTime('now', new \DateTimeZone('WIB'));
or without a time zone:
$time = new \DateTime('now');
回答2:
First, Find out the timezone for Indonesia from List Of Supported TimeZones - php Manual
Then, make it common for all places using config.php file. Add 'timeZone'=>'Your TimeZone', after components section.
Example : config.php
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
.
.
.
],
'timeZone'=>'Asia/Kolkata',
'params' => $params,
];
来源:https://stackoverflow.com/questions/36982547/time-is-wrong-when-get-datetime-in-yii2