100

I am reading a folder with lots of files.

How can I get the creation date of a file. I don't see any direct function to get it.

There are filemtime and filectime.

And if the file hasn't been modified, what will happen?

0

4 Answers 4

136

Use filectime. For Windows it will return the creation time, and for Unix the change time which is the best you can get because on Unix there is no creation time (in most filesystems).

Note also that in some Unix texts the ctime of a file is referred to as being the creation time of the file. This is wrong. There is no creation time for Unix files in most Unix filesystems.

6
  • Returns the time the file was last changed, or FALSE on failure. The time is returned as a Unix timestamp.
    – zod
    Commented Dec 9, 2010 at 21:51
  • 1
    @zod If you read a little further than the first lines you may understand more. Go into the comments a little too.
    – Alin P.
    Commented Dec 9, 2010 at 21:59
  • 1
    That is not create date. Thats all i said :-)
    – zod
    Commented Dec 9, 2010 at 23:04
  • 11
    filemtime for Linux is better, more precise, as filectime is changed during owner, permission change asn other operations. You will get more chances to get creation time on Linux using filemtime Commented Oct 6, 2014 at 13:05
  • Would it be faster to run this php function or to pull a MySQL field for last updated? In my case image path is store in DB and already running a select query
    – drooh
    Commented Jun 2, 2019 at 2:39
34

This is the example code taken from the PHP documentation here: https://www.php.net/manual/en/function.filemtime.php

// outputs e.g.  somefile.txt was last changed: December 29 2002 22:16:23.

$filename = 'somefile.txt';

if (file_exists($filename)) {

    echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}

The code specifies the filename, then checks if it exists and then displays the modification time using filemtime().

filemtime() takes 1 parameter which is the path to the file, this can be relative or absolute.

5
  • 4
    Rakesh please explain your code, don't just post some code and expect people to understand it.
    – Persijn
    Commented Feb 11, 2018 at 21:25
  • 2
    While this code-only post might answer the question, please add an explanation of why it does so. This will help future readers evaluate the answer for their situation. Commented Feb 12, 2018 at 5:23
  • 3
    The function filemtime() returns the modification time.
    – Stefan
    Commented Jan 9, 2020 at 10:03
  • Michael, since meaning of filemtime() function is not obvious, the code deserves some comment.
    – Radium
    Commented Feb 5, 2020 at 11:29
  • The code specifies the filename, then checks if it exists and then displays the modification time using filemtime(). filemtime() takes 1 parameter which is the path to the file, this can be relative or absolute.The example above is copied from the PHP documentation here php.net/manual/en/function.filemtime.php Commented Sep 27, 2020 at 8:46
10

Unfortunately if you are running on linux you cannot access the information as only the last modified date is stored.

It does slightly depend on your filesystem tho. I know that ext2 and ext3 do not support creation time but I think that ext4 does.

1
-4

I know this topic is super old, but, in case if someone's looking for an answer, as me, I'm posting my solution.

This solution works IF you don't mind having some extra data at the beginning of your file.

Basically, the idea is to, if file is not existing, to create it and append current date at the first line. Next, you can read the first line with fgets(fopen($file, 'r')), turn it into a DateTime object or anything (you can obviously use it raw, unless you saved it in a weird format) and voila - you have your creation date! For example my script to refresh my log file every 30 days looks like this:

if (file_exists($logfile)) {
    $now = new DateTime();
    $date_created = fgets(fopen($logfile, 'r'));

    if ($date_created == '') {
        file_put_contents($logfile, date('Y-m-d H:i:s').PHP_EOL, FILE_APPEND | LOCK_EX);
    }

    $date_created = new DateTime($date_created);
    $expiry = $date_created->modify('+ 30 days');
    
    if ($now >= $expiry) {
        unlink($logfile);
    }
}
1
  • 8
    Why do you need to modify or even delete a file to get the date? That does not sound like this is a solution to the original problem
    – Nico Haase
    Commented Mar 29, 2018 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.