什么是 Unix 时间戳,为什么要使用它?
什么是 Unix 时间戳?在 PHP 中,当处理日期时,函数 strtotime()
输出一些整数值——那是什么?我试图了解这一点,但我无法得到满意的答案,尤其是为什么我们需要使用 strtotime()
转换日期.
什么是 Unix 时间戳
简单地说,Unix 时间戳是一种将时间跟踪为运行总秒数的方法.这个计数从 1970 年 1 月 1 日 UTC 的 Unix 纪元开始.因此,Unix 时间戳只是特定日期和 Unix 纪元之间的秒数.还应该指出的是,无论您在地球上的哪个位置,这个时间点在技术上都不会改变.这对于在线和客户端的动态和分布式应用程序中跟踪和分类过时信息的计算机系统非常有用.许多网站管理员使用 Unix 时间戳的原因是它们可以一次代表所有时区.如需更多信息,请阅读维基百科文章.
什么是strtotime()
,它有什么用处
顾名思义,strtotime()
函数用于将日期字符串转换为Unix时间戳(str to time).
来自 [strtotime()
的 PHP 手册文档] 2:p><块引用>
strtotime — 将任何英文文本日期时间描述解析为 Unix 时间戳
例如,假设您想要获取日期 2013 年 12 月 25 日
的 Unix 时间戳,那么您可以像这样使用 strtotime()
:
echo strtotime(2013 年 12 月 25 日"),
";//=>1387909800
strtotime()
还可以处理相对时间和日期格式.例如,考虑以下情况:
echo strtotime("+1 月"), "
";//=>1390980039echo strtotime("下个月的最后一天"), "
";//=>1391152839
这些是一些基本示例.strtotime()
也可以处理非常复杂的日期格式.有关详细信息,请参阅文档.
什么时候应该使用时间戳
无论地区如何,Unix 时间戳的解释都是相同的,并且无论时区如何,都是从同一时间点计算的.如果您有一个在多个时区使用的 Web 应用程序,并且您需要日期/时间来反映各个用户的设置,请使用时间戳.
对于strtotime()
,多用于日期格式之间的转换.由于 strtotime()
几乎可以解析任何日期字符串,因此它用于将日期字符串转换为时间戳.获得时间戳后,您可以使用 date()
随意格式化它,或类似的功能.
strtotime()
的限制在 32 位系统上,整数的最大值为 2,147,483,647.可以这种方式表示的最远时间是 2038 年 1 月 19 日星期二 03:14:07 UTC.这也称为 2038 年问题.
请参阅 PHP 手册中的此注释:
<块引用>时间戳的有效范围通常是从 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 和更新的版本克服了这个限制.
使用 DateTime 对象
如果您使用 1901 年 12 月 13 日
到 2038 年 1 月 19 日
范围之外的日期,请考虑使用 PHP 的 DateTime 对象可以处理更广泛的日期.DateTime 在任一方向上都可以代表大约 2930 亿年.
DateTime 类在 PHP 版本 >= 5.2.0 上可用.如果您运行的 PHP 版本高于 >= 5.2.0,那么在处理日期和时间时应该使用 DateTime.这是最好的方法.如果您使用的是旧 PHP 版本,请已经升级.5.3.0 之前的任何东西都是古老的.
- 使用
DateTime::__construct()
或DateTime::createFromFormat()
来创建DateTime
对象.请注意,DateTime::createFromFormat()
仅适用于 PHP >= 5.3.使用此方法,您可以解析日期和时间奇怪的字符串,否则可能无法使用strtotime()
- 使用
DateTime::format()
将您的DateTime
对象转换为您可能想要使用的任何日期格式的方法
这里有一些关于 DateTime 的好文章:
- 为什么 PHP DateTime 如此受欢迎
- PHP 正确方法:日期和时间
还有一本书:
- php|建筑师的日期和时间编程指南(由 Derick Rethans 编写,他是 PHP 核心日期扩展的贡献者)
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()
.
What is a Unix Timestamp
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 that they can represent all time zones at once. For more information, read the Wikipedia article.
What is strtotime()
and how is it useful
As the name suggests, strtotime()
function is used to convert a date string to a Unix timestamp (str to time).
From the [PHP manual documentation for strtotime()
] 2:
strtotime — Parse about any English textual datetime description into a Unix timestamp
For example, say you wanted to get the Unix timestamp for the date 25 December 2013
, then you'd use strtotime()
like so:
echo strtotime("25 December 2013"), "
"; // => 1387909800
strtotime()
can also handle relative time and date formats. For example, consider the following:
echo strtotime("+1 month"), "
"; // => 1390980039
echo strtotime("last day of next month"), "
"; // => 1391152839
These are some basic examples. strtotime()
can handle very complex date formats, too. See the documentation for more information.
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 the 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.
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.
Limitations of 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.
See this note in the PHP manual:
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.
Use DateTime objects
If you're working with dates outside the 13 Dec 1901
to 19 Jan 2038
range, then consider using PHP's DateTime objects which can work with a much wider range of dates. DateTime can represent approximately 293 billion years in either direction.
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.
- Use either
DateTime::__construct()
orDateTime::createFromFormat()
to create aDateTime
object. Note thatDateTime::createFromFormat()
is only available on PHP >= 5.3. Using this method, you can parse date and time weird strings that might otherwise not be possible withstrtotime()
- Use
DateTime::format()
method to convert yourDateTime
object to any date format you might want to work with
Here are some good articles on DateTime:
- Why PHP DateTime rocks
- PHP The Right Way: Date and Time
And a book for the shelf:
- php|architect's Guide to Date and Time Programming (written by Derick Rethans, who is a contributor to PHP's core date extension)
相关文章