3

I have this code:

$expiration_date='2041-07-14'
           $epoch_timestamp_expiration_date =  strtotime($expiration_date);
       //Get 7 days
       $seven_days_ago=7*86400;
       //subtract seven days from the expiration date.
       $epoch_timestamp_expiration_date-=$seven_days_ago;
       //Format the new expiration date - 7 days ago
       $formatted_epoch_time=date('Y-m-d',$epoch_timestamp_expiration_date);       
       //Todays format.
       $today=date('Y-m-d', time());
       //Todays miliseconds
       $today_secs=strtotime($today);
       //Subtracting expiration date in epoch secs from todays secs
       $diff_secs = abs($epoch_timestamp_expiration_date-$today_secs);
       //Finding the number of days between the two
       $days=floor($diff_secs/86400);
       //Printing output
  echo "<br/><br/>Days: ". $days; 
     echo "<br/><br/>Today: ".$today;
    echo "<br/><br/>Expiration Date : ".$expiration_date;
     echo "<br/><br/>Expiration Date 7 days ago: ".$formatted_epoch_time ; 

      //Is cache near to expire. 7 days closer to the expiration date. 
        if ($epoch_timestamp_expiration_date>$today_secs) {
     echo "<br/><br/>The site isnt about to expire ";
            return "<br/><br/>Cache date isnt about to expire ".$days;
        }

When the output gets echoed, I get this:

Days: 15634

Today: 2012-10-14

Expiration Date : 2041-07-14

Expiration Date 7 days ago: 1969-12-25

Why?

now if i swap the value the parameter:

$expiration_date='2013-07-14';

I get:

Days: 266

Today: 2012-10-14

Expiration Date : 2013-07-14

Expiration Date 7 days ago: 2013-07-07

The site isnt about to expire

Dmitry Makovetskiyd
  • 6,942
  • 32
  • 100
  • 160
  • 4
    http://en.wikipedia.org/wiki/Year_2038_problem – Mat Oct 14 '12 at 14:53
  • [PHP & mySQL: Year 2038 Bug: What is it? How to solve it?](http://stackoverflow.com/questions/2012589/php-mysql-year-2038-bug-what-is-it-how-to-solve-it) – coderabbi Oct 14 '12 at 15:06

1 Answers1

5

It's because the expiration_date is beyond the Unix Timestamp (2038-01-19): http://en.wikipedia.org/wiki/Unix_time

http://php.net/manual/en/function.date.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 (e.g. Windows)."

Niborb
  • 774
  • 5
  • 15
  • hmmm..interesting..and would they change it when we reach 2020 for example – Dmitry Makovetskiyd Oct 14 '12 at 14:54
  • 2
    @Dmitry - You should be running 64-bit Linux and PHP by then.... but use PHP DateTime objects, which are already available in PHP, and which support a date range that would keep Paleontologists and Cosmologists happy – Mark Baker Oct 14 '12 at 14:55
  • Yes 64-bit PHP is the way to go :) – Niborb Oct 14 '12 at 14:56
  • @Raidenace - you can build PHP as 64-bit now, but most hosts only build as 32-bit – Mark Baker Oct 14 '12 at 15:34
  • @MarkBaker: I know buddy...My PHP_INT_SIZE is 8 :) – raidenace Oct 14 '12 at 15:40
  • I'm running 3 different versions on my Windows dev box: IIS/32-bit PHP; 64-bit Apache/64-bit PHP and 32-bit Apache/32-bit PHP; not to mention all the variants in virtual boxes - but DateTime objects should still be a preferred option when working with dates and times in PHP; not just because of the range, but also the timezone handling – Mark Baker Oct 14 '12 at 15:46