It is necessary to get the time when the file is accessed and modified. In the section, you learned how to access and modification Time in PHP.
Here I am going to cover one quick article about three file functions fileatime( ) , filemtime( ), and filectime( ) which returns the time of last access, modification, and metadata change of a file respectively.
What is fileatime()?
fileatime() function gives the access time of the file is updated when you open a file or when it is used for other operations.
$lastAccess = fileatime('test.php');
What is filemtime()?
filemtime() function gives the modification time of the file when you change the contents or save the file.
$lastModification = filemtime('test.php');
What is filectime()?
$lastChange = filectime('test.php');
We can use one more file function
touch() which sets the access and modification time of file.We can say touch() function returns file touch time.
bool touch ( string $filename [, int $time = timestamp [, int $atime ]] )
Here, First parameter filename specifies the name of the file.second parameter is the modification time that will change. The time parameter is directly responsible for the prescribed time. The time parameter is optional so you can ignore it you want to set the current time.
Let’s have a look of touch function used as:
touch('test.php'); // set modification time to now touch('test.php',$timestamp); // set modification time to $timestamp
The second parameter in touch() function is optional and without the second parameter, touch() set current date and time of modification of a file.To set a modification time of file with the specific value, pass that value as a timestamp to touch( ) function as a second parameter.
Now Let’s get some basic idea about fileatime( ) , filemtime( ), and filectime( ) functions:
The fileatime( ) function returns the last time a file was opened for reading or writing.
The filemtime( ) function returns the last time the contents of files were changed.
The filectime( ) function returns the last time a contents or metadata like an owner or permissions of files were changed.
NOTE: Each function gives the time as an timestamp.
echo "Last Modified Time of FILE:".strftime('%c',filemtime($_SERVER['SCRIPT_FILENAME']));
Above example will gives the actual last modification time of File.
Last Modified Time of FILE: 03/04/12 21:32:18