filemtime()
The filemtime() function is a built-in PHP function that returns the last modification time of a file. This function returns a Unix timestamp representing the
The filemtime() function is a built-in PHP function that returns the time a file's contents were last modified, as a Unix timestamp. It is one of PHP's filesystem inspection functions and is commonly used for cache invalidation, "last updated" labels, and detecting whether a file has changed since you last read it.
This chapter covers the syntax, the return value and the difference between filemtime() and the related filectime() / fileatime() functions, formatting the result for humans, error handling, and the result-caching gotcha that trips up almost everyone.
Syntax
filemtime(string $filename): int|false$filename— path to the file (or directory) to inspect.- Return value — the modification time as a Unix timestamp (seconds since January 1 1970 UTC) on success, or
falseon failure.
The "modification time" (mtime) changes whenever the file's content is written. It does not change when the file is merely read, nor when only its metadata (permissions, owner) changes — that is the inode change time, reported by filectime().
A basic example
Because a Unix timestamp is just a large integer, you typically pass it to date() to produce a readable string:
<?php
$filename = __FILE__; // inspect this script itself
$timestamp = filemtime($filename);
$readable = date('F d Y H:i:s', $timestamp);
echo "The file was last modified on $readable";__FILE__ is a magic constant that always points at the current script, so this snippet runs without you having to create a separate file. filemtime() returns the timestamp, and date() formats it (here: full month name, day, year, then 24-hour time).
Always handle failure
filemtime() returns false when the file does not exist or cannot be read, and it also emits a warning. Because false is loosely equal to 0, never feed the raw result straight into date() — check it first, ideally after confirming the file exists with file_exists():
<?php
$filename = 'does-not-exist.txt';
if (!file_exists($filename)) {
echo "File not found.";
} else {
$timestamp = filemtime($filename);
if ($timestamp === false) {
echo "Could not read the modification time.";
} else {
echo "Last modified: " . date('Y-m-d H:i:s', $timestamp);
}
}Use the strict === false comparison: a legitimate timestamp is never false, but == false would also catch the (impossible-in-practice) timestamp 0.
mtime vs. ctime vs. atime
PHP exposes three different file timestamps. Knowing which one to use prevents subtle bugs:
| Function | Returns | Changes when… |
|---|---|---|
filemtime() | modification time | the file's contents are written |
filectime() | inode change time | contents or metadata (permissions, owner, name) change |
fileatime() | access time | the file is read (often disabled for performance) |
For "show me when this was last edited," you almost always want filemtime().
The caching gotcha: clearstatcache()
PHP caches the results of filesystem functions like filemtime() for the duration of a request. If you change a file and then call filemtime() again in the same script, you may get the stale value. Call clearstatcache() to force a fresh lookup:
<?php
$filename = tempnam(sys_get_temp_dir(), 'demo');
file_put_contents($filename, 'first write');
$first = filemtime($filename);
sleep(1);
touch($filename); // bump the mtime
clearstatcache(); // without this, you may still see $first
$second = filemtime($filename);
echo $second > $first ? "mtime updated\n" : "mtime unchanged (cached)\n";
unlink($filename);Here touch() updates the modification time, and clearstatcache() ensures the second filemtime() reflects it.
A practical use: cache busting
A common real-world use is appending the file's mtime to an asset URL so browsers re-download it only when the file actually changes:
<?php
$cssPath = __FILE__; // pretend this is 'styles.css'
$version = filemtime($cssPath);
echo "/assets/styles.css?v=$version";Each time the CSS is edited, $version changes, busting the browser cache automatically.
Conclusion
filemtime() reports when a file's contents were last modified, as a Unix timestamp, and returns false on failure. Pair it with date() to format the result, guard against false, remember clearstatcache() when re-reading a file you just changed, and reach for filectime() or fileatime() when you need change or access time instead. For a full set of file statistics in one call, see stat().