Time is wrong when get datetime in yii2

五迷三道 提交于 2019-12-20 05:13:23

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!