filemtime

filemtime($filename)

Returns the time of the last change of the file content as a timestamp (the number of seconds since 1 Jan 1970), on error false.

Parameters

  • $filename – a path to a file or folder.

Examples

date_default_timezone_set("Europe/Prague");
$file = sys_get_temp_dir() . "/garth-example.txt";
file_put_contents($file, "x");
// the change time set for the example
touch($file, mktime(14, 30, 0, 10, 1, 2026));
echo date("j M Y H:i", filemtime($file)); // 1 Oct 2026 14:30
// the age of the file in seconds
$now = mktime(15, 0, 0, 10, 1, 2026);
echo $now - filemtime($file); // 1800
// cache: refresh when the file is older than an hour
var_dump($now - filemtime($file) > 3600); // bool(false)
// the file version in the URL, after a change the browser downloads the new file
echo "style.css?v=" . filemtime($file); // style.css?v=1790857800
unlink($file);

Things to watch out for

  • PHP remembers the result during the script. After a write to the file in the same script, PHP 7.4 returns the old time without clearstatcache(), PHP 8.3 and newer already the new one. When the file is changed by another program, clearstatcache() is always needed.
  • The timestamp is in UTC, date() turns it into a readable date according to the set time zone.
  • Copying or uploading via FTP can set the change time to the moment of the transfer. Do not use it as the date the content was created.
  • The filectime() function returns something else: on Linux the time of the change of the file information (permissions, name), on Windows the creation time.

Related functions:
filesize()
date()
mktime()
time()
touch()
filectime()