E
E
edik892016-06-24 10:38:24
Yii
edik89, 2016-06-24 10:38:24

How to make sure that dates do not get lost in YII2?

To display dates on the site, I use Yii::$app->formatter->asDate(). The data type in mysql database is DATE. For some reason, every night around 11:00 pm, the dates jump one day forward. For example, it was 01 Aug - 14 Aug 2016 became 02 Aug - 15 Aug 2016. After a few hours everything is back to normal. What can be wrong?
I output the dates like this:

if ($this->end_date != false) {
      return Yii::$app->formatter->asDate($this->start_date, 'php:d M') . ' - ' . Yii::$app->formatter->asDate($this->end_date, 'php:d M Y');	
    } else {
      return Yii::$app->formatter->asDate($this->start_date, 'php:d M');
    }

The settings specify the same timezone as on the server:
'components' => [
        'formatter' => [
            'defaultTimeZone' => 'Europe/Moscow'
        ],
...

I noticed that if I display the date using Yii::$app->formatter->asDate($post['time'], 'php:Ymd H:i:s'), then the time is an hour ahead. Through date('Ymd H:i:s', strtotime($post['time'])) everything is displayed perfectly.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
edik89, 2016-06-24
@edik89

The problem was related to the clock change on October 26, 2014. It was solved by updating the libicu52 library.

V
VitGun, 2016-06-24
@VitGun

timezones on the DB server and WEB server are different, for example

D
Dmitry Donkovtsev, 2016-06-24
@Demetriy

Вы указываете только у компонента formatter, лучше задавать в целом по системе, т.е. заходите в main.php и одним из параметров:
+
если у вас какой-нибудь шаблон advanced, т.е. несколько папок на проект (common, console, frontend), то это надо указать во всех конфигах.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question