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) . "<br>";
}

The article concludes that using filemtime combined with date provides a straightforward way to obtain and display file modification times in PHP.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

date functionfilemtimephp-tutorialfile-modification
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

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.