Returns the last modification time of the file, in the format of Unix timestamp (second level).
gettimeofday($return_float = false)
Returns the current time information. If $return_float is set to true , a floating point number will be returned, including seconds and microseconds, with a higher precision than time() .
Use filemtime to get the last modification time of the file (seconds).
Use gettimeofday(true) to get the current time (floating number of seconds, including microseconds).
Calculate the difference between the two and get the difference in seconds between the file modification time and the current time.
The difference can be converted into more readable formats such as seconds, minutes, hours, or even more detailed.
<?php
// Assume file path
$file = '/path/to/your/file.txt';
// Get the last time of the file modification(Second)
$file_mtime = filemtime($file);
// Get the current time(包含微Second的Floating point numberSecond)
$current_time = gettimeofday(true);
// Calculate the time difference,单位为Second(Floating point number)
$time_diff = $current_time - $file_mtime;
// Output time difference,reserve3Decimal number
echo "The last time the file is modified is now approximately " . number_format($time_diff, 3) . " Second。";
// If you want to display it as a more understandable format,Convert can be performed
$minutes = floor($time_diff / 60);
$seconds = $time_diff % 60;
echo "\nApproximately {$minutes} point {$seconds} Second前修改的。";
?>
filemtime returns the Unix timestamp of the file, which does not contain the microsecond part, so the time accuracy is to seconds.
gettimeofday(true) returns the current time with microseconds, with higher accuracy.
Since the file modification time only has the second level accuracy, the microsecond part will not affect the file modification time when actually calculating the time difference, but can be used to improve the accuracy of the current time.
Suitable for monitoring whether a file has been recently modified or detected for update duration.
<?php
$file = '/path/to/your/file.txt';
$max_interval = 3600; // 1Hour,单位Second
$file_mtime = filemtime($file);
$current_time = gettimeofday(true);
$time_diff = $current_time - $file_mtime;
if ($time_diff > $max_interval) {
echo "The file has exceeded 1 Hour没有被修改。";
} else {
echo "Files are recent 1 Hour内有修改。";
}
?>
Combining gettimeofday(true) and filemtime functions can easily implement the exact time difference calculation of file modification time and current time. filemtime provides the file last modification time, gettimeofday provides high-precision current time, and the combination of the two realizes a simple and practical file time monitoring function.
<?php
$url = 'https://gitbox.net/path/to/resource';
echo "VisitedURLyes:" . $url;
?>