phpyear2038

touch function in PHP 7 64-bit does not handle dates beyond 2038


On PHP 7.2.14 64-bit, on Windows, touch() cannot set a file's date to a date after 2038. filemtime() will read such a file's date fine (date was set with an external tool).

Is this expected behavior?
Is it possible to write code that changes a file's date in a Y2038 friendly way?
Issue doesn't seem to be in PHP Bug Tracking : Search for "2038"

Code sample:

$filename = 'C:\\Test\\File.txt';
for ($i = 2035; $i <= 2040; $i++) {
    $t = mktime(1,1,1,1,1,$i);
    echo 'Date: '.date('D, d M Y H:i:s', $t).'<br>';
    touch($filename, $t);
    clearstatcache(true, $filename);
    $file = filemtime ($filename);
    echo 'File: '.date('D, d M Y H:i:s', $file).'<br><br>';
}

Ouput:

Date: Mon, 01 Jan 2035 01:01:01
File: Mon, 01 Jan 2035 01:01:01

Date: Tue, 01 Jan 2036 01:01:01
File: Tue, 01 Jan 2036 01:01:01

Date: Thu, 01 Jan 2037 01:01:01
File: Thu, 01 Jan 2037 01:01:01

Date: Fri, 01 Jan 2038 01:01:01
File: Fri, 01 Jan 2038 01:01:01

Date: Sat, 01 Jan 2039 01:01:01
File: Tue, 25 Nov 1902 18:32:45  <-- Wrong

Date: Sun, 01 Jan 2040 01:01:01
File: Wed, 25 Nov 1903 18:32:45  <-- Wrong

If I manually set the same file's date to 2040, the following works as expected:

$file = filemtime ('C:\\Test\\File.txt');
echo 'File: '.date('D, d M Y H:i:s', $file);

Output:

Date: Sun, 01 Jan 2040 01:01:01

Solution

  • I reported it as a bug in PHP and it was fixed the next day!

    Bug #78241 is fixed starting with PHP 7.3.8 & 7.2.21.