Backend Development 3 min read

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 formatting the timestamp with date, and provides code examples for handling both single and multiple files.

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

PHP's filemtime function returns the last modification timestamp of a file when given its path.

To use it, assign the file path to a variable and call filemtime($file_path) , then format the timestamp with date() for readable output.

$file_path = 'path/to/file.txt';
$modification_time = filemtime($file_path);
echo "File last modified: " . date('Y-m-d H:i:s', $modification_time);

The article also shows how to process multiple files by storing their paths in an array, iterating with foreach , retrieving each file's modification time, and printing the filename with basename() and formatted date.

$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 "File '" . basename($file_path) . "' last modified: " . date('Y-m-d H:i:s', $modification_time) . "
";
}

These examples demonstrate retrieving and displaying modification times for single or multiple files, helping developers understand and apply the filemtime function in PHP projects.

backendPHPTutorialfilemtimefile modification time
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.