why strotime returns negative value in php?

送分小仙女□ 提交于 2019-12-11 10:50:36

问题


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


回答1:


It's to do with the Unix Epoch.

See: date() and time()

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




回答2:


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.




回答3:


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.




回答4:


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




回答5:


I am still learning with the timestamp,

google unix timestamp -> http://en.wikipedia.org/wiki/Unix_time

defined as the number of seconds elapsed since midnight proleptic Coordinated Universal Time (UTC) of January 1, 1970




回答6:


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.



来源:https://stackoverflow.com/questions/3469474/why-strotime-returns-negative-value-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!