PHP: Detect Time Of Last Modification Of A File

Do you need to find out in PHP, when a file was modified? Then you should take a look at the PHP-function filemtime. This function expects a filename as parameter and returns the modification-time of this file.
Here is an example:

< ?php
echo filemtime('anyfile.html');
?>

This PHP-script will output the timestamp of the last modification of the file anyfile.html.
You can also format this by using the date-function:

<?php
// Gets modification time and formats it
echo date("Y-m-d h:i:s",filemtime('anyfile.html'));
?>

And if you want to include this function in a php-file to display its last modification time, you could try something like this:

<?php
/*
1. Uses basename() to get the filename from $_SERVER['PHP_SELF']
2. Uses filemtime to get modification-time for this file
3. Uses date to format the time.
*/
echo date("Y-m-d h:i:s",filemtime(basename($_SERVER['PHP_SELF'])));
?>

Have fun 😉

(Visited 1,050 times, 1 visits today)

Leave a Comment