PHP file modification time in milliseconds

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");


Try this simple command:

ls --full-time 'filename'

and you can see the file timestamp precision is not second, it is more precise. (using Linux, but don't think it differs in Unix) but I still don't know of a PHP function for getting precise timestamp, maybe you can parse the result of the system call.


If the file system is ext4 (common on more recent unixes / linuxes like Ubuntu) or ntfs (Windows), then the mtime does have sub-second precision.

If the file system is ext3 (or perhaps others; this was the standard a while ago and is still used by RHEL), then the mtime is only stored to the nearest second. Perhaps that old default is why PHP only supports mtime to the nearest second.

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

(I have tested the following on a system with an English locale only; the "human readable" output of stat may differ, or the strtotime behaviour may differ on non-English locales. It should work fine in any timezone, as the output of stat includes a timezone specifier which is honoured by 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 (e.g. 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";
    }
}

Tags:

Php

Filemtime