php 什么是 Unix 时间戳,为什么要使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20822821/
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 03:35:05  来源:igfitidea点击:

What is a Unix timestamp and why use it?

phptimestampstrtotime

提问by Amal Murali

What is a Unix timestamp? In PHP, when working with dates, the function strtotime()outputs some integer value -- what is that? I tried to learn about this but I couldn't get satisfactory answer, especially why do we need to convert dates using strtotime().

什么是 Unix 时间戳?在 PHP 中,当处理日期时,函数会strtotime()输出一些整数值——那是什么?我试图了解这一点,但我无法得到满意的答案,尤其是为什么我们需要使用strtotime().

回答by Amal Murali

What is a Unix Timestamp

什么是 Unix 时间戳

Simply put, the Unix timestamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the Unix timestamp is merely the number of seconds between a particular date and the Unix Epoch. It should also be pointed out that this point in time technically does not change no matter where you are located on the globe. This is very useful to computer systems for tracking and sorting dated information in dynamic and distributed applications both online and client side. The reason why Unix timestamps are used by many webmasters is because they can represent all time zones at once. For more information, read the the Wikipedia article.

简单地说,Unix 时间戳是一种将时间作为运行总秒数来跟踪的方法。该计数从 1970 年 1 月 1 日 UTC 的 Unix 纪元开始。因此,Unix 时间戳只是特定日期和 Unix Epoch 之间的秒数。还应该指出的是,无论您位于地球上的哪个位置,从技术上讲,这个时间点都不会改变。这对于计算机系统在在线和客户端的动态和分布式应用程序中跟踪和分类日期信息非常有用。许多站长使用Unix时间戳的原因是因为它们可以一次代表所有时区。有关更多信息,请阅读维基百科文章

What is strtotime()and how is it useful

什么是strtotime()以及它如何有用

As the name suggests, strtotime()function is used to convert a date string to a Unix timestamp (str to time).

顾名思义,strtotime()函数用于将日期字符串转换为 Unix 时间戳(str to time)。

From the PHP manual documentation for strtotime():

来自PHP 手册文档strtotime()

strtotime— Parse about any English textual datetime description into a Unix timestamp

strtotime— 将任何英文文本日期时间描述解析为 Unix 时间戳

For example, say you wanted to get the Unix timestamp for the date 25 December 2013, then you'd use strtotime()like so:

例如,假设您想获取 date 的 Unix 时间戳25 December 2013,那么您可以strtotime()像这样使用:

echo strtotime("25 December 2013"), "\n";       // => 1387909800

strtotime()can also handle relative time and date formats. For example, consider the following:

strtotime()还可以处理相对时间和日期格式。例如,请考虑以下情况:

echo strtotime("+1 month"), "\n";               // => 1390980039
echo strtotime("last day of next month"), "\n"; // => 1391152839

These are some basic examples. strtotime()can handle very complex date formats, too. See the documentationfor more information.

这些是一些基本的例子。strtotime()也可以处理非常复杂的日期格式。有关更多信息,请参阅文档

When should I use a timestamp

我什么时候应该使用时间戳

A Unix timestamp is interpreted the same regardless of region, and is calculated from the same point in time regardless of time zone. If you have a web application that is used over multiple timezones and you need date / time to reflect individual users' settings, use a timestamp.

无论区域如何,Unix 时间戳的解释都是相同的,并且无论时区如何,都从同一时间点计算。如果您有一个在多个时区使用的 Web 应用程序,并且您需要日期/时间来反映单个用户的设置,请使用时间戳。

In the case of strtotime(), it is mostly used to convert between date formats. Since strtotime()can parse almost any date string, it's used to convert the date string into a timestamp. Once you have the timestamp, you can format it however you wish, using date(), or similar functions.

在 的情况下strtotime(),它主要用于日期格式之间的转换。由于strtotime()几乎可以解析任何日期字符串,因此它用于将日期字符串转换为时间戳。获得时间戳后,您可以根据需要对其进行格式化,使用date()或类似函数。

Limitations of strtotime()

限制 strtotime()

On a 32-bit system, the maximum value of an integer is 2,147,483,647. The furthest time that can be represented this way is 03:14:07 UTC on Tuesday, 19 January 2038. This is also known as Year 2038 problem.

在 32 位系统上,整数的最大值是 2,147,483,647。可以用这种方式表示的最远时间是 2038 年 1 月 19 日星期二 UTC 时间 03:14:07。这也称为2038 年问题

See this note in the PHP manual:

请参阅PHP 手册中的此注释:

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.

时间戳的有效范围通常是从 1901 年 12 月 13 日星期五 20:45:54 UTC 到 2038 年 1 月 19 日星期二 03:14:07 UTC。(这些是对应于 32 位有符号整数的最小值和最大值的日期。)此外,并非所有平台都支持负时间戳,因此您的日期范围可能被限制为不早于 Unix 纪元。这意味着例如 1970 年 1 月 1 日之前的日期在 Windows、某些 Linux 发行版和一些其他操作系统上将不起作用。PHP 5.1.0 和更新版本克服了这个限制。

Use DateTime objects

使用 DateTime 对象

If you're working with dates outside the 13 Dec 1901to 19 Jan 2038range, then consider using PHP's DateTimeobjects which can work with a much wider range of dates. DateTime can represent approximately 293 billion years in either direction.

如果您正在使用13 Dec 1901to19 Jan 2038范围之外的日期,那么请考虑使用 PHP 的DateTime对象,它可以使用更广泛的日期范围。DateTime 可以在任一方向上表示大约 2930 亿年。

The DateTime class is available on PHP versions >= 5.2.0. If you are running a PHP version that's above >= 5.2.0, then you should use DateTime when working with dates and times. It's the best way to go. If you're having an older PHP version, upgrade already. Anything before 5.3.0 is ancient.

DateTime 类在 PHP 版本 >= 5.2.0 上可用。如果您运行的 PHP 版本高于 >= 5.2.0,那么在处理日期和时间时应该使用 DateTime。这是最好的方法。如果您使用的是较旧的 PHP 版本,请升级。5.3.0 之前的任何东西都是古老的。

Here are some good articles on DateTime:

这里有一些关于 DateTime 的好文章:

And a book for the shelf:

和书架上的一本书: