0

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
Goozak
  • 508
  • 1
  • 5
  • 21
  • 1
    Smells like it may be a bug but have you searched [PHP bugs](https://bugs.php.net/) to see if it has already been reported? Please [edit] your question and include your OS as well as the version of PHP. – Dave Jun 21 '19 at 18:58

2 Answers2

1

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.

Goozak
  • 508
  • 1
  • 5
  • 21
0

It's seems that you run into Year 2038 problem, see here: https://en.wikipedia.org/wiki/Year_2038_problem You cannot encode times after 03:14:07 UTC on 19 of January 2038, so you fall back to 13th of December 1901

Marco
  • 3,470
  • 4
  • 23
  • 35
  • Yes, that's what I'm testing. 64-bit version of PHP is able to handle these dates (as stated, `filemtime`, among others, does). I'm wondering why `touch()` doesn't. – Goozak Jun 21 '19 at 19:00