PHP filectime() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

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');
CallWhat 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

ParameterDescription
filenameRequired. The path to the file or directory.

Return Value

Return ValueDescription
intThe change time as a Unix timestamp.
falseReturned 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);

How It Works

  1. file_put_contents() creates the file, which sets its change time to now.
  2. filectime() returns that time as a Unix timestamp.
  3. time() minus the timestamp gives the age of the change in seconds.
  4. 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.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_missing_ctime.txt';

$ctime = @filectime($path);

if ($ctime === false) {
    echo 'Cannot read change time', PHP_EOL;
} else {
    echo 'Changed at: ', $ctime, PHP_EOL;
}

How It Works

  1. The path points to a file that does not exist.
  2. filectime() has nothing to read and returns false.
  3. The @ operator hides the warning PHP would normally show.
  4. The script prints a friendly message instead of a bad timestamp.

Example 3 — Change Time vs Modification Time

Set an old modification time with touch(). The modification time goes back, but the change time stays recent.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_filectime_cmp.txt';
file_put_contents($path, 'Hello');

touch($path, 1700000000);
clearstatcache();

echo 'Modified: ', filemtime($path), PHP_EOL;
echo 'Changed after modified: ', var_export(filectime($path) > filemtime($path), true), PHP_EOL;

unlink($path);

How It Works

  1. touch() sets the modification time to an old date in 2023.
  2. On Unix, changing the time also updates the inode change time to now.
  3. On Windows, filectime() is the creation time, which is also recent.
  4. In both cases filectime() is later than filemtime(), so the comparison is true.

Frequently Asked Questions

It returns the last change time of a file as a Unix timestamp. On Unix it is the inode change time, and on Windows it is the creation time.
Only on Windows. On Linux and macOS it is the last time the file's data or metadata, like permissions, was changed.
filemtime() changes when the file content is modified. filectime() on Unix also changes when permissions, ownership or the name change.
Pass the result to date(), for example date('Y-m-d H:i:s', filectime($file)).
The file does not exist or PHP cannot read its information. Check the path and permissions, and use file_exists() first if needed.

Next: filegroup()

filegroup() returns the numeric group ID that owns a file. On Linux and macOS this is the .

filegroup() 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