89

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?

franzlorenzon
  • 5,333
  • 6
  • 31
  • 55
zod
  • 11,159
  • 23
  • 64
  • 102

4 Answers4

115

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.

Alin Purcaru
  • 40,402
  • 12
  • 70
  • 88
  • Returns the time the file was last changed, or FALSE on failure. The time is returned as a Unix timestamp. – zod Dec 09 '10 at 21:51
  • @zod If you read a little further than the first lines you may understand more. Go into the comments a little too. – Alin Purcaru Dec 09 '10 at 21:59
  • 1
    That is not create date. Thats all i said :-) – zod Dec 09 '10 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` – Fedir RYKHTIK Oct 06 '14 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 Jun 02 '19 at 02:39
  • consider calling `clearstatcache()` before using filemtime to clear the php metadata cache. – M Rostami Apr 08 '20 at 12:28
20

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.

Rakesh Dongarwar
  • 337
  • 4
  • 10
  • 4
    Rakesh please explain your code, don't just post some code and expect people to understand it. – Persijn Feb 11 '18 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. – Tom Brunberg Feb 12 '18 at 05:23
  • 1
    The function `filemtime()` returns the modification time. – Stefan Jan 09 '20 at 10:03
  • Michael, since meaning of filemtime() function is not obvious, the code deserves some comment. – Radium Feb 05 '20 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 https://www.php.net/manual/en/function.filemtime.php – Harvey Dobson Sep 27 '20 at 08:46
8

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.

mrwooster
  • 22,109
  • 11
  • 34
  • 44
-3

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);
            }
        }
SDukla
  • 17
  • 5
    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 Mar 29 '18 at 11:40