Backend Development 3 min read

How to Use PHP filemtime to Retrieve File Modification Time

This article explains how to use PHP's filemtime function to retrieve a file's last modification timestamp, demonstrates single‑file and multiple‑file examples, and shows how to format the timestamp with the date function for readable output.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Use PHP filemtime to Retrieve File Modification Time

PHP's filemtime function returns the last modification time of a file as a Unix timestamp. By passing the file path as an argument, developers can obtain this timestamp and then format it using date for human‑readable output.

Example 1 shows how to define a file path, call filemtime , and echo the formatted modification time.

$file_path = 'path/to/file.txt'; // 文件路径
$modification_time = filemtime($file_path); // 获取文件的最后修改时间

echo "文件最后修改时间:" . date('Y-m-d H:i:s', $modification_time); // 将时间戳转换为可读格式

Example 2 demonstrates processing multiple files by storing their paths in an array, iterating with foreach , retrieving each file's modification time, and printing the file name together with the formatted timestamp.

$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 article concludes that using filemtime combined with date provides a straightforward way to obtain and display file modification times in PHP.

BackendPHPdate functionphp tutorialfile-modificationfilemtime
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.