PHP filemtime() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, filemtime() returns the time a file was last modified, as a Unix timestamp.

A Unix timestamp is the number of seconds since January 1, 1970. You can pass it to date() to show a readable time, or compare it with time() to see how old a file is.

Syntax

Call filemtime() with the values you need. Start with this line.

PHP
$time = filemtime('notes.txt');
CallWhat it does
filemtime('notes.txt')Returns the timestamp of the last change to the file contents.
date('Y-m-d H:i:s', filemtime('notes.txt'))Turns the timestamp into a readable date and time.
time() - filemtime('cache.html')Gives the age of the file in seconds, handy for cache checks.

filemtime() reports when the file content was written. For the time the file information changed, such as permissions, use filectime(), and for the last read time use fileatime(). PHP caches the result, so call clearstatcache() when you read the same file again after changing it.

Library declarations
PHP
filemtime(string $filename): int|false

Parameters

ParameterDescription
filenameRequired. The path to the file or folder you want to check.

Return Value

Return ValueDescription
intReturns the last modification time as a Unix timestamp.
falseReturns false on failure, for example when the file does not exist.

PHP Examples

Example 1 — Show When a File Changed

Create a file and print the time it was last modified in a readable format.

PHP
<?php
$file = __DIR__ . '/mtime_demo.txt';
file_put_contents($file, 'Hello');

$time = filemtime($file);

echo 'Last modified: ', date('Y-m-d H:i:s', $time), PHP_EOL;

unlink($file);

How It Works

  1. file_put_contents() writes the file, which sets its modification time to now.
  2. filemtime() returns that time as a Unix timestamp.
  3. date() converts the timestamp into a year-month-day hour:minute:second format.
  4. The time you see will match the moment you run the script.

Example 2 — Check That the File Exists

filemtime() returns false for a missing file, so test it before calling date().

PHP
<?php
$file = __DIR__ . '/missing.txt';

$time = @filemtime($file);

if ($time === false) {
    echo "File not found", PHP_EOL;
} else {
    echo 'Last modified: ', date('Y-m-d', $time), PHP_EOL;
}

How It Works

  1. The file missing. txt does not exist.
  2. filemtime() returns false, and @ hides the warning.
  3. Without this check, date() would receive false and print a date from 1970.
  4. The script shows a clear message instead.

Example 3 — Set a Time and Read It Back

Use touch() to give a file a fixed modification time, then read it with filemtime().

PHP
<?php
$file = __DIR__ . '/dated.txt';
file_put_contents($file, 'report');

touch($file, strtotime('2026-01-15 10:00:00'));
clearstatcache();

echo 'Modified: ', date('Y-m-d H:i', filemtime($file)), PHP_EOL;
echo 'Older than one day: ', (time() - filemtime($file)) > 86400 ? 'yes' : 'no', PHP_EOL;

unlink($file);

How It Works

  1. strtotime() turns the text date into a timestamp.
  2. touch() sets the modification time of the file to that value.
  3. clearstatcache() makes sure PHP reads the fresh value and not an old cached one.
  4. Subtracting from time() gives the file age in seconds, and 86400 is the number of seconds in one day.

Frequently Asked Questions

It returns the Unix timestamp of the last time a file's contents were modified.
Pass the result to date(), for example date('Y-m-d H:i:s', filemtime($file)).
filemtime() tracks changes to the file content. filectime() tracks changes to the file information, such as permissions or ownership, and also changes when the content changes.
It returns false and raises a warning. Check the file with file_exists() or compare the result with === false.
PHP caches file information for the duration of the request. Call clearstatcache() before filemtime() if the file changed during the script.

Next: fileowner()

fileowner() returns the user id of the owner of a file.

fileowner() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful