문제

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

도움이 되었습니까?

해결책

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

다른 팁

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 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.

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

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

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top