PHP time() 是否返回 GMT/UTC 时间戳?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4812170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:28:27  来源:igfitidea点击:

Does PHP time() return a GMT/UTC Timestamp?

php

提问by Jiew Meng

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

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

回答by deceze

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

time返回一个 UNIX 时间戳,它与时区无关。由于 UNIX 时间戳表示自 1970 UTC以来的秒数,您可以说它是 UTC,但它确实没有时区。



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 1296096875in 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. 1296096875in Tokyo is 2011-01-27 11:54:35, in London it's 2011-01-27 02:54:35and in New York it's 2011-01-26 21:54:35.

确切地说,UNIX 时间戳在任何给定时间在全世界都是相同的值。在撰写本文时,它1296096875位于东京、伦敦和纽约。要将其转换为“人类可读”的时间,您需要指定要在哪个时区显示它。1296096875在东京是2011-01-27 11:54:35,在伦敦是,在2011-01-27 02:54:35纽约是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 pointon this line can be expressed as:

这条线上的绝对点可以表示为:

  • 1296096875
  • Jan. 27 2011 02:54:35 Europe/London
  • 1296096875
  • 2011 年 1 月 27 日 02:54:35 欧洲/伦敦

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.

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

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:35means almost nothing on the timeline pictured above. Jan. 27 2011 02:54:35is 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.

人类可读的挂钟时间更复杂,而且由于它在日常使用中被缩写和部分省略,因此变得更加复杂。02:54:35在上图的时间线上几乎没有任何意义。Jan. 27 2011 02:54:35已经更具体了,但仍然可能意味着这条线上的各种不同点。“当时钟于 2011 年 1 月 27 日在欧洲伦敦敲响 02:54:35 时”现在终于成为这条线上的一个明确的绝对点,因为只有一个时间点是正确的。

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.

因此,时区是“挂钟时间”的“修饰符”,它是使用日历和小时/分钟/秒表示法表达独特的绝对时间点所必需的。如果没有时区,这种格式的时间戳是不明确的,因为时钟在 2011 年 1 月 27 日的 02:54:35 在全球每个国家/地区的不同时间敲响。

A UNIX timestamp inherently does not have this problem.

UNIX 时间戳本质上没有这个问题。



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.

要将 UNIX 时间戳转换为人类可读的挂钟时间,您需要指定希望显示时间的时区。要将挂钟时间转换为 UNIX 时间戳,您需要知道挂钟时间是哪个时区应该在。您必须每次都在每次此类转换时都包含时区,或者您将默认时区设置为与date_default_timezone_set.

回答by Julian

Since PHP 5.1.0 (when the date/time functions were rewritten), every call to a date/time function will generate a E_NOTICE if the timezone isn't valid, and/or a E_WARNING message if using the system settings or the TZ environment variable.

从 PHP 5.1.0 开始(当日期/时间函数被重写时),如果时区无效,每次调用日期/时间函数都会生成 E_NOTICE 和/或如果使用系统设置或 TZ 的 E_WARNING 消息环境变量。

So in order to get a UTC timestamp you should check what the current timezone is and work off of that orjust use:

因此,为了获得 UTC 时间戳,您应该检查当前时区是什么并解决它仅使用:

$utc_str = gmdate("M d Y H:i:s", time());
$utc = strtotime($utc_str);

回答by Steve Howard

http://us3.php.net/time

http://us3.php.net/time

"Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)."

“返回自 Unix 纪元(1970 年 1 月 1 日 00:00:00 GMT)以来以秒为单位测量的当前时间。”

So I believe the answer to your question is yes.

所以我相信你的问题的答案是肯定的。

回答by Chris Thompson

From the documentation

文档

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

返回自 Unix 纪元(1970 年 1 月 1 日 00:00:00 GMT)以来以秒数衡量的当前时间。

回答by OXiGEN

One of the comments claimed: "both time() and strtotime(gmdate("M d Y H:i:s", time())) return the same result"

其中一条评论声称:“time() 和 strtotime(gmdate("M d YH:i:s", time())) 都返回相同的结果”

Since I wasn't sure about that, I ran a test:

由于我不确定这一点,我进行了测试:

$now = strtotime(gmdate("Y-m-d H:i:s", time()));
$now2 = time();
echo ' now='.$now.' now2='.$now2.' diff='.($now - $now2);

Output was:

输出是:

 now=1536824036 now2=1536806036 diff=18000

Diff is 18000 seconds = 5 hours = the timezone offset for the server running the test.

差异是 18000 秒 = 5 小时 = 运行测试的服务器的时区偏移。