2

I used this function to find remaining days in PHP

function days_until($date){
     return (isset($date)) ? floor((strtotime($date) - time())/60/60/24)+1 : FALSE;
}

It's almost perfect except when it has to return any value greater than 7547

An example case is when I tried to find days_until('2038-01-20'), today being 2017-05-22, returns -17308. While the same for days_until('2038-01-19') returns 7547. Can anyone tell me whats going on here? Its like strtotime($date) isn't even working, and returning 0.

Can anyone solve this, with an explanation, please?

Siddhant Rimal
  • 975
  • 2
  • 11
  • 32
  • 2
    Follow this question, [Date difference](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) And [this](https://stackoverflow.com/questions/2040560/finding-the-number-of-days-between-two-dates) too. – Bhaumik Pandhi May 22 '17 at 16:16
  • 2
    `strtotime()` works with signed 32bit integers (on 32bit versions of PHP), so it overflows somewhere on 19 January 2038. See the [Year 2038 problem](https://en.wikipedia.org/wiki/Year_2038_problem). – Decent Dabbler May 22 '17 at 16:16

1 Answers1

1

As stated on a note from the PHP Manual for strtotime():

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

...

For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.

rbock
  • 625
  • 5
  • 15