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

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

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.

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

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.