php timestamp max

  I encountered a bug today, and the validity period value was incorrectly obtained. After positioning and tracking, it was found that the validity period has a value, as follows:

$expireDate = 2133999048;  //2037-08-16 09:30:48

 But after adding 1 year to the timestamp, the value of $expireDate is false, not the correct timestamp.

<? php
 $expireDate = strtotime ("+1 year", $expireDate );
 echo ( $expireDate ); // 31536000 Convert time format to 1971-01-01 08:00:00

Search on the Internet, it is said to be a 32-bit unix timestamp vulnerability, called Y2K38 vulnerability. 64-bit systems are not affected by this.

Y2K38 Vulnerability 

Y2K38, also known as the Unix Millennium Bug, affects all 32 -bit systems in PHP and other programming languages ​​that use UNIX timestamp integers to record time.
The maximum time that an integer variable can hold is January 19 , 2038 03:14:07 . After this time, the integer value will overflow.
From January 01 , 1970 to Tuesday , January 19 , 2038 at 03:14:07 AM UTC over 2 ^ 31 1 . _ _ 2 ^ 31 - 1 is 0x7FFFFFFF. I believe many programmers have seen it. In 32 -bit systems, this represents the largest signed integer. If you use it to represent the number of seconds, it is roughly equivalent to 68.1 years, which is exactly the number from 1970 to 2038 .

A valid time in php is from 1970-01-01 07:00:00 - 2038-01-19 03:14:07 . This one
About the generation of unix timestamp in php
<?php
echo strtotime('2038-01-19 03:14:07').PHP_EOL; // 2147483647
echo strtotime('1970-01-01 07:00:00'); // 0"Y-m-d", $expireDate);//25200

So how to judge that a timestamp can return to the correct time smoothly. There are usually two ways to
reverse the thinking method.

function is_timestamp($timestamp) {
    if(strtotime(date('m-d-Y H:i:s', $timestamp)) === $timestamp) {
        return $timestamp;
    } else {
        return false;
    }
}

Ordinary integer range judgment

$is_unixtime = ctype_digit($str) && $str <= 2147483647;

This method is relatively simple and crude.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325126649&siteId=291194637