Saturday, October 25, 2025
HomeLanguagesPHP | filemtime( ) Function

PHP | filemtime( ) Function

The filemtime() function in PHP is an inbuilt function which is used to return the last time of a specified file when its content was modified. The filemtime() function returns the last time the file was changed as a Unix Timestamp on success and False on failure.

The filename is passed as a parameter to the filemtime() function. The result of the filemtime() function is cached and a function called clearstatcache() is used to clear the cache.

Syntax:

filemtime($filename)

Parameters: The filemtime() function in PHP accepts only one parameter $filename. It specifies the file which you want to check.

Return Value: It returns the last time of a file when its content was modified as a Unix Timestamp on success and False on failure.

Errors And Exception:

  1. The time resolution may differ from one file system to another.
  2. This function doesn’t works on some unix systems which have access time updates are disabled to increase performance.

Examples:

Input : echo filemtime("gfg.txt");
Output : 1525159574

Input : echo "Last modified: ".date("F d Y H:i:s.", 
                              filemtime("gfg.txt"));
Output : Last modified: May 1 2018 07:26:14.

Below programs illustrate the filemtime() function.

Program 1:




<?php
  
// checking last time the contents
// of a file were changed
echo filemtime("gfg.txt");
  
?>


Output:

1525159574

Program 2:




<?php
  
// checking last time the contents
// of a file were changed
echo filemtime("gfg.txt");
  
// checking last time the contents of
// a file were changed and formatting
// the output of the date 
echo "Last modified: ".date("F d Y H:i:s.", 
                      filemtime("gfg.txt"));
?>


Output:

1525159574
Last modified: May 1 2018 07:26:14.

Reference:
http://php.net/manual/en/function.filemtime.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS