Example Analysis of Y2K38 Vulnerability Solution in php

  • 2021-07-18 07:35:55
  • OfStack

This paper analyzes the solution of Y2K38 vulnerability in php. Share it for your reference. The specific analysis is as follows:

Y2K38, also known as Unix Millennium Bug, this vulnerability will affect PHP and other programming languages that use UNIX timestamp integers to record time on all 32-bit systems.

The maximum time that a variable of integer type can be saved is 03:14:07 on January 19, 2038. After this time, the integer value will overflow.

From January 1, 1970 to 03:14:07 am UST on Tuesday, January 19, 2038, it exceeded 2 ^ 31 1. 2 ^ 31 1 is 0x7FFFFFFF, which I believe many programmers have seen. In a 32-bit system, this represents the largest signed integer. If you use it to express the number of seconds, it is equivalent to about 68.1 years, which is exactly the number from 1970 to 2038.

Dates after 03:14:07 on January 19, 2038 will be overflowed on 32-bit systems.


<?php
$date = '2040-01-01 12:00:00';
echo strtotime($date);           //  Empty   Overflow 
echo date('Y-m-d H:i:s', strtotime($date)); // 1970-01-01 00:00:00
?>

So will 64-bit systems be affected?

Theoretically not, it is strongly recommended to carry out 1 test. The farthest date that can be saved under 64-bit system is 21 times to 29.2 billion years of the current age of the universe.

On 32-bit machines, you can use the DateTime class to solve this problem. (PHP 5.2 introduced this class and extended a few methods in version 5.3.)

The code is as follows:


<?php
$date = '2040-01-01 12:00:00';
$dt = new DateTime($date);
echo $dt->format('U');      // 2209032000
echo $dt->format('Y-m-d H:i:s'); // 2040-01-01 12:00:00 
?>

I hope this article is helpful to everyone's study of PHP programming.


Related articles: