PHP: Find Out The Modification Time Of A File

Do you want to find out the modification time of a file in your PHP script? Well, in this article you’ll find out how easy this is. 😉


Advertisement

First of all you need the filename. in the example below, I’ll show you how to find out the modification time of the file itself, which contains the PHP script. But of course you can use any other accessible file on your server.
Next I’ll use the filemtime() function to determine the UNIX timestamp of the last modification of the file.
Them I’ll output the date and time by using the date() function and echo it on the screen.

<?php
/*
We use the filename of the actual script for this example.
You can use any other filename, of course
*/
$fName = basename($_SERVER['PHP_SELF']);
/*
With filemtime we can find out the date and time of the 
last modification of this file as a UNIX timestamp.
*/
$fTime = filemtime($fName);
// Now output the timestamp as a formatted date
echo 'Last Update: '.date("Y-m-d H:i",$fTime);
?>

Or, you can write this in a more compact form in just one line of code:

<?php
echo 'Last Update: '.date("Y-m-d H:i",filemtime(basename($_SERVER['PHP_SELF'])));
?>

For more information about the filemtime() function, go to the php.net documentation.


Advertisement
(Visited 39 times, 1 visits today)

Leave a Comment