Back to Top

How to get File access and modification Time

How to get File access and modification Time

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

filectime() function gives the time of file when change file attributes, such as file owner, permissions, or moved to another file system. It is also update and change the file modification time.

$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

Also Read:

Method Chaining in PHP
To Unzip file and move to destination in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

An Introduction to Gulp JS

Posted on 1 year ago


Top 10 JavaScript Charting Libraries

Posted on 4 years ago


Difference between echo and print in PHP

Posted on 7 years ago


Hello world

Updated 5 days ago

Nick Carter

In depth Understanding of Laravel Core

Updated 2 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 4 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 5 years ago