PHP fileatime() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fileatime() returns the last time a file was accessed (read), as a Unix timestamp. A Unix timestamp is the number of seconds since January 1, 1970.

Use it to find files that nobody has opened for a long time, for example when you clean up old cache files. You can turn the number into a readable date with date() or gmdate().

Syntax

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

PHP
$accessed = fileatime('data.txt');
CallWhat it does
fileatime('data.txt')Returns the last access time of data.txt as a Unix timestamp.
date('Y-m-d H:i:s', fileatime('data.txt'))Formats the access time as a readable date and time.
time() - fileatime('data.txt')Gives the number of seconds since the file was last accessed.

Many systems turn off or reduce access-time updates to make the disk faster, so the value may be older than you expect. PHP also caches file information, so call clearstatcache() when you check the same file more than once.

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

Parameters

ParameterDescription
filenameRequired. The path to the file or directory.

Return Value

Return ValueDescription
intThe last access 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 — Read the Access Time

Set a known access time with touch() and read it back with fileatime().

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

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

$accessed = fileatime($path);

echo 'Timestamp: ', $accessed, PHP_EOL;
echo 'Date (UTC): ', gmdate('Y-m-d H:i:s', $accessed), PHP_EOL;

unlink($path);

How It Works

  1. file_put_contents() creates a small demo file.
  2. touch() sets both the modification time and the access time to 1700000000.
  3. clearstatcache() clears PHP's cached file information so the new value is read.
  4. gmdate() turns the timestamp into a readable UTC date.

Example 2 — A File That Does Not Exist

For a missing file, fileatime() returns false and raises a warning. Check the result before using it.

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

$accessed = @fileatime($path);

if ($accessed === false) {
    echo 'Cannot read access time', PHP_EOL;
} else {
    echo 'Accessed at: ', $accessed, PHP_EOL;
}

How It Works

  1. The path points to a file that does not exist.
  2. fileatime() returns false because it has nothing to look at.
  3. The @ operator hides the warning that PHP would normally print.
  4. The if statement checks for false and prints a clear message.

Example 3 — Compare Access and Modification Time

Set different times for modification and access, then compare them with filemtime() and fileatime().

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

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

echo 'Modified: ', filemtime($path), PHP_EOL;
echo 'Accessed: ', fileatime($path), PHP_EOL;
echo 'Accessed later: ', var_export(fileatime($path) > filemtime($path), true), PHP_EOL;

unlink($path);

How It Works

  1. touch() takes the modification time first and the access time second.
  2. The access time is set one hour (3600 seconds) after the modification time.
  3. filemtime() and fileatime() return the two different values.
  4. The comparison confirms that the file was accessed after it was changed.

Frequently Asked Questions

It returns the time a file was last accessed, as a Unix timestamp. It returns false if the information cannot be read.
Pass the timestamp to date() or gmdate(), for example date('Y-m-d H:i:s', fileatime($file)).
Many servers mount disks with noatime or relatime, which reduces access-time updates. On those systems, reading a file does not always change the value.
fileatime() is the last access, filemtime() is the last content change and filectime() is the last inode change on Unix, or the creation time on Windows.
PHP caches file information. Call clearstatcache() before you read the value again.

Next: filectime()

filectime() returns the time a file was last changed, as a Unix timestamp. On Linux and ma.

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