为什么 strotime 在 php 中返回负值?
我正在使用 strtotime 将日期转换为 unix 时间戳。 年、日期和日作为不同的编码值,我使用下面的代码来生成时间戳。
$year = '1961';
$month = '2';
$day = '15';
$date = $year."-".$month."-".$day;
echo strtotime($date);
上面的代码对我来说打印:-27648000。如果年份高于 1970 年,则会打印出正结果。我仍在学习时间戳,如果有人可以帮助我。主要目的是将日期转换为 unix 时间戳。
问题是为什么它给出负面结果,我编码不好吗?我也尝试过 mktime,但结果仍然相同。
谢谢, 坦美
I am using strtotime to convert a date to a unixtime stamp.
Year, date and day comes as different values to code and I am using the below code to produce the timestamp.
$year = '1961';
$month = '2';
$day = '15';
$date = $year."-".$month."-".$day;
echo strtotime($date);
The above code prints : -27648000 for me. If the year is above 1970 it prints out positive results. I am still learning with the timestamp, if any one can help me out. The main aim is to convert a date to unix timestamp.
The questions is why it gives negative results, Am I coding it bad!? I am also tried mktime, but still the same result.
Thanks,
Tanmay
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这与 Unix 纪元有关。
请参阅:
date()
和time()
时间戳的有效范围通常是从1901 年 12 月 13 日星期五 20:45:54 GMT 至 2038 年 1 月 19 日星期二 03:14:07 GMT。 (这些日期对应于 32 位有符号整数的最小值和最大值)。但是,在 PHP 5.1.0 之前,在某些系统上此范围限制为 01-01-1970 到 19-01-2038
It's to do with the Unix Epoch.
See:
date()
andtime()
The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems
这是预期的行为。
strtotime
返回 UNIX 时间戳,即自 1970 年 1 月 1 日以来的秒数(不考虑闰秒)秒)。对于在此之前的日期,它将返回负数。That's the expected behavior.
strtotime
returns a UNIX timestamp, the number of seconds since Jan 1 1970 (not considering leap seconds). For dates before that, it will return a negative number.Unix 时间从 Unix 纪元开始,即 1970 年 1 月 1 日午夜。因此,在此之前的任何日期都将导致返回负值。
Unix time starts at the Unix epoch which is Midnight on Jan 1, 1970. So any date before that will result in a negative value being returned.
这是 Unix 时间戳。 Unix/PHP 基准日期为 1970 年 1 月 1 日 00:00 UST,时间戳以秒为单位。如果为负数,则为基准日期之前的秒数;如果为正,则之后的秒数
This is a Unix timestamp. The Unix/PHP base date is January 1, 1970 at 00:00 UST, and the timestamp is measured in seconds. If negative, it is the number of seconds before the base date; if positive, the number of seconds after
google
unix timestamp
-> http://en.wikipedia.org/wiki/Unix_timegoogle
unix timestamp
-> http://en.wikipedia.org/wiki/Unix_timehttp://en.wikipedia.org/wiki/Unix_time
Unix 时间,或 POSIX 时间,是一种描述时间点的系统,定义为自 1970 年 1 月 1 日午夜预产协调世界时 (UTC) 以来经过的秒数,不包括闰秒。它被广泛使用,不仅在类 Unix 操作系统中,而且在许多其他计算系统和文件格式中。它既不是时间的线性表示,也不是 UTC 的真实表示(尽管它经常被误认为是两者),因为它表示的时间是 UTC,但不代表标准 UTC 闰秒(例如 1998 年 12 月 31 日 23:59:60 )...
1970 年 1 月 1 日之前的时间为负值,因为它们发生在 UTC 开始之前。
http://en.wikipedia.org/wiki/Unix_time
Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight proleptic Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds. It is used widely, not only in Unix-like operating systems, but also in many other computing systems and file formats. It is neither a linear representation of time nor a true representation of UTC (though it is frequently mistaken for both), as the times it represents are UTC but do not represent standard UTC leap seconds (e.g. December 31, 1998 23:59:60)...
Times before 1/1/1970 are negative values as they occured before the start of UTC.