281 of 410 menu

The fileatime Function

The fileatime function returns the last access time of a file as a Unix timestamp. The file path is passed as a parameter. If the file does not exist, the function returns false.

Syntax

fileatime(string $filename): int|false

Example

Get the last access time of a file:

<?php $res = fileatime('test.txt'); echo $res; ?>

Code execution result:

1672531200

Example

Check if the file exists before getting the access time:

<?php $filename = 'nonexistent.txt'; if (file_exists($filename)) { echo fileatime($filename); } else { echo 'File not found'; } ?>

Code execution result:

'File not found'

Example

Convert Unix timestamp to a readable format:

<?php $timestamp = fileatime('test.txt'); echo date('Y-m-d H:i:s', $timestamp); ?>

Code execution result:

'2023-01-01 00:00:00'

See Also

  • the filemtime function,
    which returns the modification time
  • the filectime function,
    which returns the creation time
  • the touch function,
    which changes the access time
byenru