Using PHP filemtime to Retrieve File Modification Time
This article explains how to use PHP's filemtime function to obtain a file's last modification timestamp, demonstrates converting the timestamp to a readable date with date(), and provides code examples for retrieving modification times of a single file as well as multiple files.
PHP's filemtime function returns the last modification timestamp of a file when provided with the file path.
The function can be combined with date to format the timestamp into a human‑readable date and time.
Example – retrieving a single file's modification time:
$file_path = 'path/to/file.txt';
$modification_time = filemtime($file_path);
echo "文件最后修改时间:" . date('Y-m-d H:i:s', $modification_time);This script defines the file path, calls filemtime to get the timestamp, and uses date to display it.
Example – retrieving modification times for multiple files:
$files = array(
'path/to/file1.txt',
'path/to/file2.txt',
'path/to/file3.txt'
);
foreach ($files as $file_path) {
$modification_time = filemtime($file_path);
echo "文件:'" . basename($file_path) . "' 最后修改时间:" . date('Y-m-d H:i:s', $modification_time) . "
";
}The loop iterates over an array of file paths, obtains each file's timestamp with filemtime , and prints the filename and formatted modification time.
In summary, by passing a file path to filemtime and optionally formatting the result with date , developers can easily access and display file modification times in PHP.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.