views:

74

answers:

6

Hi,

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

A: 

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.

John Conde
+2  A: 

It's to do with the Unix Epoch.

http://uk2.php.net/manual/en/function.date.php and http://php.net/manual/en/function.time.php

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

Blair McMillan
@Blair McMillan, Thanks for your explanation and links to php manual. It really helped to understand well about the timestamp. @Everyone, Thanks for your time and support with my learning life.
jtanmay
@Blair McMillan, this got me bit more curious, so is there a way to get the timestamp before 1901, I tried with strtotime, but it returns me 0 always. Is there a way to get the timestamp for the previous dates?
jtanmay
UNIX timestamps are limited to 32 bit integers on 32 bit machines so your effective range is 1901-2038 or so. Use http://php.net/datetime instead if you need a larger range or move to a 64-bit platform. (http://bugs.php.net/bug.php?id=50913)
Blair McMillan
@Blair McMillan: Thanks for your reply. I also did look around, and found a ADODB library which would give me timestamp beyond the range. http://sourceforge.net/projects/adodb/files/ . Again I am not sure how it works. Just to mention it over here, so some one can be helped. Thanks
jtanmay
And just to add to above comment, its the adodb-time.inc.php file from the zip which was useful to me.
jtanmay
A: 

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

Mark Baker
+2  A: 

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.

Artefacto
A: 

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

Col. Shrapnel
A: 

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.

Bryon Hibbetts