PHP file modification time in milliseconds

I am there, I am currently writing a unit test, which claims that the file has not received any changes. Test code execution takes less than one second, so I would like to know if it is possible to restore the file modification time in milliseconds. The filemtime () function returns the UNIX timestamp in seconds.

My current solution uses the sleep (1) function, which will assure me that 1 second has passed before checking whether it has been changed or not. I do not like this solution, as it greatly slows down the test.

I cannot claim equality of content via get_file_contents (), since the data that can be rewritten will be the same.

I guess this is not possible, right?

+6
php filemtime
source share
4 answers

AFAIK UNIX timestamp accuracy is seconds, so this may not be the case.

BTW, note that PHP caches the return value of filemtime() internally, so clearstatcache() should be called earlier.

An alternative method may be to modify (or delete) the contents of the file first so that you can easily identify the changes. Since the state of the system should remain unchanged after each test, it would be advisable to restore the original contents of the file after running unit test.

+2
source share

Try this simple command:

 ls --full-time 'filename' 

and you can see that the accuracy of the file’s timestamp is not the second, it’s more accurate. (using Linux, but I don’t think it is different on Unix) but I still don’t know about the PHP function to get the exact timestamp, maybe you can analyze the result of the system call.

+4
source share
 function getTime($path){ clearstatcache($path); $dateUnix = shell_exec('stat --format "%y" '.$path); $date = explode(".", $dateUnix); return filemtime($path).".".substr($date[1], 0, 8); } 

GetTime ("MyFile");

+4
source share

If the file system is ext4 (common for later unixes / linux, such as Ubuntu) or ntfs (Windows), then mtime has subnet precision.

If the ext3 file system (or perhaps others, it was the standard time ago and is still used by RHEL), then mtime only saved in the next second. Perhaps the old default option is that PHP only supports mtime until the next second.

To get the value in PHP, you need to call an external utility, since PHP itself does not support it.

(I tested the following only on a system with English, the "custom" stat output may be different, or strtotime behavior may be different in non-English languages. It should work fine in any time zone, since the stat output includes the time zone specifier that strtotime adheres strtotime .)

 class FileModTimeHelper { /** * Returns the file mtime for the specified file, in the format returned by microtime() * * On file systems which do not support sub-second mtime precision (such as ext3), the value * will be rounded to the nearest second. * * There must be a posix standard "stat" on your path (eg on unix or Windows with Cygwin) * * @param $filename string the name of the file * @return string like microtime() */ public static function getFileModMicrotime($filename) { $stat = `stat --format=%y $filename`; $patt = '/^(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)\.(\d+) (.*)$/'; if (!preg_match($patt, $stat, $matches)) { throw new \Exception("Unrecognised output from stat. Expecting something like '$patt', found: '$stat'"); } $mtimeSeconds = strtotime("{$matches[1]} {$matches[3]}"); $mtimeMillis = $matches[2]; return "$mtimeSeconds.$mtimeMillis"; } } 
+1
source share

All Articles