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

phpfileatime(string $filename): int|false

Example

Get the last access time of a file:

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

Code execution result:

php1672531200

Example

Check if the file exists before getting the access time:

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

Code execution result:

php'File not found'

Example

Convert Unix timestamp to a readable format:

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

Code execution result:

php'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