Using PHP filemtime to Retrieve File Modification Time

This tutorial explains how to use PHP's filemtime function to obtain a file's last modification timestamp, convert it to a readable date format with the date function, and provides code examples for processing both a single file and multiple files.

php Courses
php Courses
php Courses
Using PHP filemtime to Retrieve File Modification Time

PHP function filemtime can be used to obtain a file's last modification time. It takes a file path as its argument and returns a Unix timestamp representing the modification time.

In PHP you can use filemtime as follows:

$file_path = 'path/to/file.txt'; // file path
$modification_time = filemtime($file_path); // get last modification time

echo "File last modification time: " . date('Y-m-d H:i:s', $modification_time);

The code first defines a variable $file_path that should be replaced with the actual path of the file you want to inspect. It then calls filemtime with that path to obtain the timestamp, and uses date to format the timestamp into a readable date‑time string.

Code Example‑1: Get File Last Modification Time

$file_path = 'path/to/file.txt';
$modification_time = filemtime($file_path);

echo "File last modification time: " . date('Y-m-d H:i:s', $modification_time);

This example assumes the file path is 'path/to/file.txt'; you can change it as needed. The date function can output the modification time in any desired format.

Code Example‑2: Get Multiple Files' Last Modification Times

$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 modification time: " . date('Y-m-d H:i:s', $modification_time) . "<br>";
}

In this example we define an array $files containing several file paths, iterate over it, retrieve each file's modification timestamp with filemtime, obtain the file name using basename, and format the timestamp with date.

Summary

This article demonstrates how to use PHP's filemtime function to retrieve a file's last modification time, convert the timestamp to a readable format with date, and provides code samples for handling both a single file and multiple files.

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.

PHPbackend-developmentdate functionfile-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

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.