<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>时区 on 施国鹏 (seth-shi)</title><link>https://sethix.com/zh-cn/tags/%E6%97%B6%E5%8C%BA/</link><description>Recent content in 时区 on 施国鹏 (seth-shi)</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Wed, 09 Dec 2020 17:30:25 +0800</lastBuildDate><atom:link href="https://sethix.com/zh-cn/tags/%E6%97%B6%E5%8C%BA/index.xml" rel="self" type="application/rss+xml"/><item><title>MySQL 中为什么使用 timestamp 可以不用关心时区.</title><link>https://sethix.com/zh-cn/posts/2020120917/</link><pubDate>Wed, 09 Dec 2020 17:30:25 +0800</pubDate><guid>https://sethix.com/zh-cn/posts/2020120917/</guid><description>之前一直有过疑惑为什么MySQL数据库存timestamp可以无视时区问题. 在业务中也是一直使用Laravel框架,内置的Migration也是使用的timestamp类型字段, 也没太关心.
开始 查看当前数据库时区 mysql&amp;gt; show variables like &amp;#34;%time_zone%&amp;#34;; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | CST | | time_zone | +08:00 | +------------------+--------+ 2 rows in set (0.30 sec) 查看表结构 mysql&amp;gt; desc timestamp_test; +--------------+-----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+-----------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | created_time | datetime | YES | | NULL | | | created_at | timestamp | YES | | NULL | | +--------------+-----------+------+-----+---------+----------------+ 3 rows in set (0.</description></item><item><title>UTC 格式的时间转换成本地的时间</title><link>https://sethix.com/zh-cn/posts/2018111517/</link><pubDate>Thu, 15 Nov 2018 17:15:54 +0800</pubDate><guid>https://sethix.com/zh-cn/posts/2018111517/</guid><description>碰到一个场景，得到一串输入时间格式，但是需要转换成我本地的时间，输入的时间格式是 UTC 时间格式：yyyy-MM-ddTHH:mm:ss.SSSZ
yyyy 表示年份四位 MM 表示月份两位表示，01 ~ 12 dd 表示多少号，01 ~ 31 T 表示日期个时间的分隔符 HH 表示小时 01 ~ 24 mm 表示分钟 01 ~ 60 ss 表示秒 01 ~ 60 SSS 表示完整毫秒数，为三位小数。 Z 是表示时区, 如下面这几种时间格式：
2018-01-01T00:00:00.000Z 默认使用 0 时区
2018-01-01T00:00:00.000+0800 东八区
转换需求，我本地在东八区，给的输入时间是: 2018-01-01T12:00:00.000+0100,因为给定的时间 +0100时区，而我在的是 +0800 时区，所以最后转换成我本地的时间就变成 2018-01-01 19:00:00.000，比输入多七个小时。 代码实现, 需要使用 Carbon 扩展包 &amp;lt;?php use Carbon\Carbon; $input = 2018-01-01T12:00:00.000+0100; // 实例化一个 Carbon 对象 $carbon = Carbon::make($input); // 转换时区 $carbon-&amp;gt;setTimezone(&amp;#39;PRC&amp;#39;); // 输出查看 echo $carbon-&amp;gt;toDateTimeString();</description></item></channel></rss>