About PHP Filemtime() Function

Use this function to automatically date time-sensitive data on your website

If your website contains time-sensitive information—or even if it doesn't—you may want to display the last time a file was modified on the website. This gives users an accurate idea of how up to date the information on a page is. You can automatically draw this information from the file itself using the filemtime () PHP function.

The filemtime() PHP function retrieves the Unix timestamp from the file.

The date function converts the Unix timestamp time. This timestamp indicates when the file was last changed.

Example Code to Display File Modification Date 

When you use this code, replace "myfile.txt" with the actual name of the file you are dating. 

// outputs myfile.txt was last modified: December 29 2002 22:16:23.

$filename = 'myfile.txt';
if (file_exists($filename)) {
    echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));

Other Uses for Filemtime() Function

In addition to time-stamping web articles, the filemtime() function can be used to select all articles older than a specified time for the purpose of deleting all old articles. It can also be used to sort articles by age for other purposes.

The function can come in handy when dealing with browser caching. You can force the download of a revised version of a stylesheet or page using the filemtime() function.

Filemtime can be used to capture the modification time of an image or other file on a remote site.

Information on Filemtime() Function

  • The results of the filemtime() function are cached. The clearstatcache() function clears the cache.
  • If the filemtime () function fails, the code returns "false."