PHP Function Reference

PHP filemtime() Function



The PHP filemtime() function returns the time when the content of the file was last modified.

Note: The results of this function are cached. Use clearstatcache() function to clear the cache.

Syntax

filemtime(filename)

Parameters

filename Required. Specify the path to the file to check.

Return Value

Returns the time the file was last modified, or false on failure. The time is returned as a Unix timestamp, which is suitable for the date() function.

Exceptions

Upon failure, an E_WARNING is thrown.

Example:

Lets assume that we have a file called test.txt in the current working directory. The example below demonstrates on using this function to get the time when the content of this file was last modified.

<?php
$filename = 'test.txt';

//getting the time when $filename 
//was last modified
if (file_exists($filename)) {
  echo "$filename was last modified: "
       .date("F d Y H:i:s.", filemtime($filename));
} else {
  echo "$filename is not found.";
}
?>

The output of the above code will be:

test.txt was last modified: September 01 2021 12:02:37.

❮ PHP Filesystem Reference