Yii2 格式化程序显示不同的日期 [英] Yii2 formatter show different date

查看:27
本文介绍了Yii2 格式化程序显示不同的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 db 查看时间,然后以时间戳格式将其保存到另一个 db.table,但每次转换它时我都会得到不同的时间:

I want to view time from db and then save it to another db.table in timestamp format, but I've get different time every time I convert it:

        print_r($sub_datetime['datetime']);echo "<br>";
        $temptime = Yii::$app->formatter->asTimestamp($sub_datetime['datetime']);
        print_r($temptime);echo "<br> ";
        $temptime2 = Yii::$app->formatter->asDatetime($temptime);
        print_r($temptime2);echo "<br> ";
        $temptime3 = Yii::$app->formatter->asTimestamp($temptime2);
        print_r($temptime3);echo "<br> ";

获取:

10-5-2015 10:00
1431252000
10-5-2015 13:00
1431262800

推荐答案

问题

asTimestamp 函数假定您指定的日期默认为 UTC,并给出 UTC 时间值.

The Issue

asTimestamp function assumes that the date you have specified is UTC by default and gives out the UTC time value.

asDatetime 函数不这么认为并获取系统时区,返回带有系统时区偏移量的日期.

asDatetime function however thinks otherwise and gets the system timezone, giving you back the date with the system timezone offset.

你有几个选择,任何一个都行

You have a few options, any one will work

  1. 在调用 asDatetime 函数之前将 yii 格式化程序时区设置为 utc Yii::$app->formatter->timeZone = 'UTC';如果需要,请提供原件).
  2. 在将时间戳传递给 asTimestamp 之前,将其偏移您的时区以将其转换为 UTC.
  3. 获得 asDatetime 的结果后,将其偏移时区以将其转换回 UTC
  1. Set the yii formatter timezone to utc Yii::$app->formatter->timeZone = 'UTC'; before calling asDatetime function (and set back to original if needed).
  2. Before passing the timestamp to asTimestamp, offset it by your timezone to convert it to UTC.
  3. After getting the result of asDatetime, offset it by timezone in order to convert it back to UTC

这篇关于Yii2 格式化程序显示不同的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆