4
date("Y",1340896077)//1340896077 are the seconds till 2012 from 1970

gives 2012 which is normal

And date("Y",1940896077)

gives 2031 which is also normal

But date("Y",2240896077)

is giving 1904 which is absolutely not at all normal .It should rather give 2041.

Now i want some explanation on time() and date() in php.

  • you can call also echo PHP_INT_MAX; to check the maximum integer and suddenly all become (sadly) clear. –  Jun 28 '12 at 15:36

4 Answers4

7

PHP uses 32bit integers.

32 bit integer is limited to 2,147,483,647

In your third example the value is overflowed. Think of it like a car KM counter. Its limited to some value (in our case 2,147,483,647) and once you go pass this value its back to 0.

This can cause the Year 2038 problem (remember Year 2000 bug? Similar concept).

In "Minutes of PHP Developers Meeting" there was a proposal to add 64bit integer, I'm not sure how far it went and what is their policy regrading time related functions.

Hope this help :)

Community
  • 1
  • 1
Dmitry Kudryavtsev
  • 16,354
  • 4
  • 25
  • 32
  • @Somebody is in trouble I can't tell you. Afaik this is going to affect a lot of language (Like C/C++ programs, they use time_t which is unsigned integer which on 32bit system is.. 32bit wide). Static typed language will have to be moved (more likely) to 64bit ints, what about PHP I'm not sure, but I saw a proposal for 64bit int in PHP (cant find the proposal). – Dmitry Kudryavtsev Jun 28 '12 at 15:27
  • 2
    PHP already works fine with 64bit ints on 64bit systems today. We just all need to move to 64bit systems within the next ~25 years. – deceze Jun 28 '12 at 15:31
1

What you're seeing is the Y2K38 bug. 2240896077 is too large for a 32bit integer and it overflows.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • what is the max value on 64 bit system –  Jun 28 '12 at 15:35
  • oh bigint of mysql is 64 bit,then why that is supported in my system –  Jun 28 '12 at 15:43
  • Possibly your PHP wasn't *compiled* as 64bit PHP. But I also don't think that MySQL's *storage requirements* have much to do with the system it's running on, contrary to PHP. – deceze Jun 28 '12 at 15:50
1

From the manual:

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 (e.g. Windows).

http://php.net/manual/en/function.date.php

Shubham
  • 21,300
  • 18
  • 66
  • 89
1

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 (e.g. Windows).

From: http://php.net/manual/en/function.date.php

psx
  • 4,040
  • 6
  • 30
  • 59
  • Amazing co-incidence :). We both hit the answer button at the same time and surprisingly the same answer. – Shubham Jun 28 '12 at 15:19