PHP date() only from 1970 - 2038 - php

First off, this is not a question about how to fix a problem because my date is outputting 1969.
This is a question about why time does not exist before 1970 or after 2038 when using date().
I've tried seaching SO and Google but all that turns up is people getting errors when using date() incorrectly, resulting in the familiar output of December 31, 1969 5:00 pm
Does anyone know why it can't go before 1970? Should we stop using date() since it will be unusable after 2038? What's the history on this? What's the work around for working with dates outside of this range?

It's 2038 problem
and look, I'm in year 1653

This is explained on the PHP manual page for date():
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).
The fact that you're getting December 31, 1969 means that you're likely supplying an odd timestamp parameter to date(), resulting in a date that isn't what you expect. Like #Mitch Wheat said in his comment, this relates to Unix time since it is relative from January 1, 1970.

Compare the number 2^31 and the number of seconds between the two date.

Related

How can i fix strotime for the year 1800

I have a little trouble with strtotime, I have a date 18 july 1868 and when I record in the database, I make $date = strtotime (18-07-1868). But when I appear it, I dated ('Y-m-d', $date) but it does not show me 18-07-1868 but 13-12-1901.
How can I fix this problem?
Thx,
From the 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.
So it depends on the platform you're running it on. It needs to be 64 bit to exceed the range stated in the manual.
You are not putting a string (single quote or double quote) in strtotime() function...
Try This...
$x = strtotime('18-07-1868');
echo date('Y-m-d', $x);

Correct input values of strtotime

I want to convert a date by the following statement
$date=date('d M Y, H:m',strtotime($date));
It printed out 09 Sep 2012, 11:09 when I tried various values of $date:
11:00 AM Sunday, 09 Sep 2012
Sunday 09 Sep 2012, 11:00 AM
09 Sep 2012 11:00 AM
What date format does the strtotime() function need? It seems to me at least one of them meets the "English textual datetime description" condition.
Valid formats for strtotime are detailed here.
From this site (http://www.tuxradar.com/practicalphp/4/5/2)
Be wary of dates such as this one: August 25, 2003, 10:26am. Although this may look perfectly well-formed, strtotime() is not able to handle it because it has commas in there - yes, they make it much more readable for us, but strtotime() gets confused handling them. If you have dates with commas in, be sure to strip them out using str_replace().
How to deal with strtotime() -> http://www.w3schools.com/php/func_date_strtotime.asp
Looks like the only way to go is to sort the elements in proper order by regular expressions and then replace the month name by corresponding number. Such a string strtotime will manage.
Note: I'm talking about PHP 5.2x
EDIT: I found the reason, why strtotime "wasn't working" was that I made a typo in the date formating string, there should be 'd M Y, H:i'

why strotime returns negative value in php?

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.

What happened on Dec 31 1969 at 7:00 PM

Every time in PHP when I make a variable such as this one:
$date = strtotime($row['date']);
$date = date("M d Y \a\\t g:i A", $date); // Mmm dd YYYY at h:mm PM/AM
and somehow row['date'] happens to be 0, the date Dec 31 1969 at 7:00 PM is displayed on the screen? Google does not tell me much, I was wondering if this date had any significances.
The Unix epoch is the time 00:00:00 UTC on 1 January 1970. This is the reference point for all time stamps. When you use PHP's date/time functions, you're always working with the number of seconds since the epoch. Time 0 is the epoch, and you (or your web server) must be on the east coast of the US, which is 5 hours behind UTC time.
I find it funny that not a single response here attempted to answer your actual question, which was (if I can paraphrase) "What is the significance of the actual date of Unix epoch time"?
I'm not an expert on the subject but basically, as I understand it, the concept of epoch time was invented in 1971. The programmers chose the arbitrary date of January 1, 1971 GMT to be epoch time. This was partly due to the fact that older computers couldn't handle large numbers so the date had to be in the recent past. Afterwards, epoch time was adjusted to be Jan 1, 1970 so as to be a nice, round number.
So basically, nothing "happened" on that date. It was an arbitrary date chosen based on the original time of the work being done.
Unix timestamps are measured in "time since the Unix Epoch", which is Midnight GMT at the end of Dec. 31 1969 (a.k.a. 00:00 GMT Jan 1 1970). Since you appear to be on Eastern Standard Time, which is GMT-5, you get 7pm Dec. 31st 1969 for a unix timestamp value of 0.
Let me guess: you live on the east coast of the USA?
PHP, like many other systems uses the Unix epoch to measure time, i.e. a value of 0 represents January 1, 1970, midnight UTC - which is the same as Dec 31 1969 at 7:00 PM Eastern Standard Time.
One format in which date objects are stored is the time in seconds that have elapsed from an arbitrary start time. Asking for a formatted version of "0" is like asking for that arbitrary start time. I don't remember why that date was chosen, but I'm sure Wikipedia does. See the article on Unix time below.
Read about Unix Time

Cocoa Core Data to PHP: Converting date

I have dates that are being stored in a database by core data. I then am using php to print out this date information but the date is coming out wrong.
When I store Aug 2, 2009 in core data it comes out in the php as Fri, August 4, 1978. How do I fix the conversion?
I'm guessing a bit here, but the limited evidence fits the hypothesis...
NSDate has an absolute reference date of 1 Jan 2001 (GMT).
PHP time() uses the Unix Epoch date of 1 Jan 1970 (GMT).
It looks like you have an offset of 31 years - or rather 978307200 seconds.
(NSTimeInterval) delta = [[NSDate dateWithTimeIntervalSinceReferenceDate:0] timeIntervalSince1970];
Solution would be to either create your dates in Cocoa with the reference date of 1970, or to add/subtract the offset in Cocoa or PHP.
James

Categories