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'