Dmitry Makovetskiyd
Dmitry Makovetskiyd

Reputation: 7053

Date inaccurate in php

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

Upvotes: 3

Views: 114

Answers (1)

Niborb
Niborb

Reputation: 784

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

Upvotes: 5

Related Questions