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'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.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.