1

On my server running under Windows XP and PHP 5.4.16, it seems like the filemtime and fileatime returns value of creation time.

$r = touch("/$fname",10,10);
if ($r===false)
   echo " FAILED ";
$stat = stat("/$fname");
print_r($stat);
$int = fileatime($fname);
$int2 = filemtime($fname);
echo "$int $int2";

The stat() return this array:

(
    [0] => 15
    [1] => 0
    [2] => 33206
    [3] => 1
    [4] => 0
    [5] => 0
    [6] => 15
    [7] => 0
    [8] => 11
    [9] => 11
    [10] => 1570657454
    [11] => -1
    [12] => -1
    [dev] => 15
    [ino] => 0
    [mode] => 33206
    [nlink] => 1
    [uid] => 0
    [gid] => 0
    [rdev] => 15
    [size] => 0
    [atime] => 11
    [mtime] => 11
    [ctime] => 1570657454
    [blksize] => -1
    [blocks] => -1
)

But echo prints creation time:

1570657955 1570657955

I know I could use the stat() instead, but why the function which should do the job do not work for me? And if it works for you, isn't the stat() slower than filemtime?

Johny Bony
  • 375
  • 2
  • 9

2 Answers2

1

Use filectime. For Windows it will return the creation time, and for Unix the change time which is the best you can get because on Unix there is no creation time (in most filesystems).

<?php

// outputs e.g.  somefile.txt was last changed: December 29 2018 22:16:23.

$filename = 'somefile.txt';
if (file_exists($filename)) {
    echo "$filename was last changed: " . date("F d Y H:i:s.", filectime($filename));
}

?>
  • Anyway the filemtime seems to be buggy. I read old comment on php site (8 years ago). "There's a deeply-seated problem with filemtime() under Windows due to the fact that it calls Windows' stat() function, which implements DST (according to this bug: http://bugs.php.net/bug.php?id=40568). The detection of DST on the time of the file is confused by whether the CURRENT time of the current system is currently under DST. " He places there some fix code. – Johny Bony Oct 10 '19 at 06:26
  • this bug reported in 2007 for PHP 5.2.1 and fix later in PHP 5.3 for ntfs but have some discrepancy in FAT volume beacause fat volume are not constant across day light saving time. I believe filectime function should solve your problem. – Anupam singh pal Oct 10 '19 at 06:36
0

Dustin's Oprea solution:

function GetCorrectMTime($filePath)
{

    $time = filemtime($filePath);

    $isDST = (date('I', $time) == 1);
    $systemDST = (date('I') == 1);

    $adjustment = 0;

    if($isDST == false && $systemDST == true)
        $adjustment = 3600;

    else if($isDST == true && $systemDST == false)
        $adjustment = -3600;

    else
        $adjustment = 0;

    return ($time + $adjustment);
} 

Tested on windows and works fine.

I also tested time difference measuring by stat() and this function and both shows 0 microseconds.

The detection of DST on the time of the file is confused by whether the CURRENT time of the current system is currently under DST. (Dustin Oprea)

https://www.php.net/manual/en/function.filemtime.php

A modified version:

function GetCorrectMTime($filePath)
{
 static $init;
 $time = filemtime($filePath);
 $adjustment = 0;
 if ( $init === null ) 
  {
  $init = true;
  $isDST = (date('I', $time) == 1);
  $systemDST = (date('I') == 1);
  $adjustment = 0;
  if($isDST == false && $systemDST == true)
      $adjustment = 3600;
  else if($isDST == true && $systemDST == false)
      $adjustment = -3600;
  }
  return ($time + $adjustment);
} 
Johny Bony
  • 375
  • 2
  • 9