PHP filemtime() | Get Last Modified Time/Date of a File

The PHP filemtime() function is used when we need to find the last time and date, when the file was modified. For example:

<?php
   echo "<p>The latest time when the file <b>codescracker.txt</b> was modified:</p>";
   echo filemtime("codescracker.txt");
   echo "<br>Or<br>";
   echo date("d F, Y H:i:s", filemtime("codescracker.txt"));
?>

The output produced by above PHP example on filemtime() function is:

php filemtime function

Since the filemtime() function returns the last modified time/date of the file, in Unix timestamp, that almost can not be understandable for normal user. Therefore, let me use the date() function to convert the time into a format that a normal user can understand.

<?php
   $file = "codescracker.txt";
   echo "<p>The file, $file last modified on:</p>";
   
   $mt = filemtime($file);
   if($mt)
   {
      echo "Time: " .date("H:i:s", $mt);
      echo "<br>";
      echo "Date: " .date("d F, Y", $mt);
   }
   else
      echo "Unable to find time, when the file last modified!";
?>

Now the output produced by above PHP example, should be:

php filemtime function example

PHP filemtime() Syntax

The syntax of filemtime() function in PHP, is:

filemtime(filename)

The function filemtime() returns False on failure.

PHP Online Test


« Previous Tutorial Next Tutorial »

Like/Share Us on Facebook 😋