-1

I have a bit of PHP code for date formatting as follows

$date =   date('Y-m-d', strtotime('2076-11-26'));                               
echo $date;

But for certain date returned value is 1970-01-01, I really confused about this matter, above is one example date which I got 1970 as year, I set the timezone as date_default_timezone_set('UTC');, but still no avail, the problem is in my live server, although its working fine in production server to.

Mohamed Akram
  • 2,244
  • 15
  • 23
  • 1
    It seems similar to this: http://stackoverflow.com/questions/3266077/php-strtotime-is-returning-false-for-a-future-date – ceyquem Nov 30 '13 at 14:17
  • You are taking the date format of boolean `FALSE` most often when you get `1970-01-01` from date: https://eval.in/74737 - better check return values first, when `strtotime` returns FALSE, it means that `strtotime` was not successful. – hakre Nov 30 '13 at 15:46

1 Answers1

1

This is because of the fact year 2076 is not in the 32-bit Unix epoch. It ends at 2038 and if you're using a 32-bit version, then you won't be able to handle dates beyond that.

From the documentation for strtotime():

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

You can use DateTime class instead, which was introduced to get around this limitation. it uses a different mechanism to store the time components, so you wouldn't face this issue. It can handle a very wide range of dates:

The same thing when done with DateTime class would look like this:

$date = new DateTime('2076-11-26');
echo $date->format('Y-m-d');

Output:

2076-11-26
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • how to check the 32bit or 64 bit in PHP, is it possible with phpinfo or via cpanel.., – Mohamed Akram Nov 30 '13 at 15:20
  • Hi, I just checked and confirmed that my shared hosting run on 64 bit, then how its possible this happen.. – Mohamed Akram Nov 30 '13 at 15:26
  • @user1711575: You did misunderstood that. Even a 64 bit PHP version has only support for Unix timestamps with `strototime` and those are 32 bit - even on a 64 bit system. What the answer is actually telling you is, that you're using the wrong function for your data. You need to verfiy first, if a function or method supports the range of data you want to operate with. `strtotime` is not fitting for dates in the year 2076. – hakre Nov 30 '13 at 15:41
  • but in my production server which is 64bit, it work perfectly, my live server also 64bit.., that why i am confused why its working in one place and not working in another place – Mohamed Akram Nov 30 '13 at 16:06