PHP time()是否返回GMT/UTC时间戳? [英] Does PHP time() return a GMT/UTC Timestamp?

查看:104
本文介绍了PHP time()是否返回GMT/UTC时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想检查time()是否返回UTC/GMT时间戳,还是我需要使用date_default_timezone_set()?

解决方案

time返回UNIX时间戳,该时间戳与时区无关.由于UNIX时间戳表示自1970年 UTC 以来的秒数,您可以说它是UTC,但实际上没有时区.


实际上,UNIX时间戳在任何给定时间在全世界范围内都是相同的值.在撰写本文时,它在东京,伦敦和纽约是1296096875.要将其转换为人类可读"的时间,您需要指定要显示在哪个时区.东京的12960968752011-01-27 11:54:35,伦敦的是2011-01-27 02:54:35,纽约是2011-01-26 21:54:35. /p>

实际上,您通常在处理时间时要处理(混合)这些概念:

  • 绝对时间点,我想称为人类历史上的
  • 本地时间,我想将其称为壁钟时间
  • 完整的时间戳,可以表示人类历史上的绝对
  • 本地壁钟时间不完整

像这样可视化时间:

-------+-------------------+-------+--------+----------------+------>
       |                   |       |        |                |
Dinosaurs died        Jesus born  Y2K  Mars colonised       ???

(不按比例缩放)

此行上的绝对点可以表示为:

  • 1296096875
  • 一月. 27 2011 02:54:35欧洲/伦敦

两种格式都以不同的符号表示相同的绝对时间点.前者是一个简单的计数器,大致从此处开始:

                          start of UNIX epoch
                                  |
-------+-------------------+------++--------+----------------+------>
       |                   |       |        |                |
Dinosaurs died        Jesus born  Y2K  Mars colonised       ???

后者是一个复杂得多但同样有效且富有表现力的计数器,大致从此处开始:

              start of Gregorian calendar
                           |
-------+-------------------+-------+--------+----------------+------>
       |                   |       |        |                |
Dinosaurs died        Jesus born  Y2K  Mars colonised       ???

UNIX时间戳很简单.它们是一个计数器,它从一个特定的时间点开始,并且每秒递增1(对于秒的官方定义).想象一下,伦敦有人在1970年1月1日午夜启动了秒表,该秒表仍在运行.这或多或少是UNIX时间戳.每个人都使用与那只秒表相同的价值.

人类可读的挂钟时间更为复杂,并且由于其在日常使用中被缩写并且部分省略的事实而变得更加复杂. 02:54:35 在上图所示的时间轴上几乎没有任何意义. Jan. 27 2011 02:54:35 已经更加具体了,但仍可能意味着这条线上的各种不同点. 当时钟于2011年1月27日上午02:54:35在欧洲伦敦 "时,现在终于是这条线上的明确绝对点,因为只有一点在这是真的的时候.

因此,时区是挂钟时间"的修饰符",使用日历和时/分/秒表示法来表示唯一的绝对时间点是必需的.如果没有时区,则采用这种格式的时间戳将是模棱两可的,因为2011年1月27日,全球每个国家/地区的时钟在不同时间响起.

UNIX时间戳本来就没有此问题.


要从UNIX时间戳转换为人类可读的挂钟时间,您需要指定想要显示时间的时区.要从壁钟时间转换为UNIX时间戳,您需要知道哪个时区应该是挂钟时间.每次转换时,您必须每次都包含时区,或者设置要与date_default_timezone_set一起使用的默认时区.

I just want to check if time() returns a UTC/GMT timestamp or do I need to use date_default_timezone_set()?

解决方案

time returns a UNIX timestamp, which is timezone independent. Since a UNIX timestamp denotes the seconds since 1970 UTC you could say it's UTC, but it really has no timezone.


To be really clear, a UNIX timestamp is the same value all over the world at any given time. At the time of writing it's 1296096875 in Tokyo, London and New York. To convert this into a "human readable" time, you need to specify which timezone you want to display it in. 1296096875 in Tokyo is 2011-01-27 11:54:35, in London it's 2011-01-27 02:54:35 and in New York it's 2011-01-26 21:54:35.

In effect you're usually dealing with (a mix of) these concepts when handling times:

  • absolute points in time, which I like to refer to as points in human history
  • local time, which I like to refer to as wall clock time
  • complete timestamps in any format which express an absolute point in human history
  • incomplete local wall clock time

Visualise time like this:

-------+-------------------+-------+--------+----------------+------>
       |                   |       |        |                |
Dinosaurs died        Jesus born  Y2K  Mars colonised       ???

(not to scale)

An absolute point on this line can be expressed as:

  • 1296096875
  • Jan. 27 2011 02:54:35 Europe/London

Both formats express the same absolute point in time in different notations. The former is a simple counter which started roughly here:

                          start of UNIX epoch
                                  |
-------+-------------------+------++--------+----------------+------>
       |                   |       |        |                |
Dinosaurs died        Jesus born  Y2K  Mars colonised       ???

The latter is a much more complicated but equally valid and expressive counter which started roughly here:

              start of Gregorian calendar
                           |
-------+-------------------+-------+--------+----------------+------>
       |                   |       |        |                |
Dinosaurs died        Jesus born  Y2K  Mars colonised       ???

UNIX timestamps are simple. They're a counter which started at one specific point in time and which keeps increasing by 1 every second (for the official definition of what a second is). Imagine someone in London started a stopwatch at midnight Jan 1st 1970, which is still running. That's more or less what a UNIX timestamp is. Everybody uses the same value of that one stopwatch.

Human readable wall clock time is more complicated, and it's even more complicated by the fact that it's abbreviated and parts of it omitted in daily use. 02:54:35 means almost nothing on the timeline pictured above. Jan. 27 2011 02:54:35 is already a lot more specific, but could still mean a variety of different points on this line. "When the clock struck 02:54:35 on Jan. 27 2011 in London, Europe" is now finally an unambiguous absolute point on this line, because there's only one point in time at which this was true.

So, timezones are a "modifier" of "wall clock times" which are necessary to express a unique, absolute point in time using a calendar and hour/minute/second notation. Without a timezone a timestamp in such a format is ambiguous, because the clock struck 02:54:35 on Jan. 27 2011 in every country around the globe at different times.

A UNIX timestamp inherently does not have this problem.


To convert from a UNIX timestamp to a human readable wall clock time, you need to specify which timezone you'd like the time displayed in. To convert from wall clock time to a UNIX timestamp, you need to know which timezone that wall clock time is supposed to be in. You either have to include the timezone every single time with each such conversion, or you set the default timezone to be used with date_default_timezone_set.

这篇关于PHP time()是否返回GMT/UTC时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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