In PHP, filectime() returns the time a file was last changed, as a Unix timestamp. On Linux and macOS it is the inode change time, and on Windows it is the creation time.
On Unix systems, the inode time updates when you change the file content, but also when you change its permissions, owner or name. Because of this, it is not the creation time on Linux, which surprises many beginners.
Syntax
Call filectime() with the values you need. Start with this line.
PHP
$changed = filectime('data.txt');
Call
What it does
filectime('data.txt')
Returns the change time of data.txt as a Unix timestamp.
date('Y-m-d H:i:s', filectime('data.txt'))
Formats the change time as a readable date and time.
time() - filectime('data.txt')
Gives the number of seconds since the file was last changed.
Do not treat filectime() as the creation time on Linux or macOS, because it changes whenever the file's data or metadata changes. On Windows it is the creation time. For the last content change, use filemtime().
Library declarations
PHP
filectime(string $filename): int|false
Parameters
Parameter
Description
filename
Required. The path to the file or directory.
Return Value
Return Value
Description
int
The change time as a Unix timestamp.
false
Returned on failure, for example when the file does not exist. A warning is also raised.
PHP Examples
Example 1 — Check If a File Was Just Changed
Create a file and confirm that its change time is very recent.
PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_filectime.txt';
file_put_contents($path, 'Hello');
clearstatcache();
$ctime = filectime($path);
$age = time() - $ctime;
echo 'Changed within the last minute: ', var_export(abs($age) < 60, true), PHP_EOL;
unlink($path);
Output
Changed within the last minute: true
How It Works
file_put_contents() creates the file, which sets its change time to now.
filectime() returns that time as a Unix timestamp.
time() minus the timestamp gives the age of the change in seconds.
abs($age) < 60 checks that the change happened within the last minute.
Example 2 — A File That Does Not Exist
For a missing file, filectime() returns false and raises a warning. Always check the result.