Pedro Alves
Pedro Alves

Reputation: 90

How do I get how long the file was created in seconds in PHP

I'm trying to create a script that deletes a file that was created 1 day ago, and I need to calculate how long was the file created in seconds, but I don't know how, can anyone help?

Upvotes: 0

Views: 763

Answers (3)

ravah
ravah

Reputation: 1

The following script displays files that are changed in +1 day, on the folder /path:

<?php
$path_to_dir ='/path/';
$timenow = time();
if ($handle = opendir($path_to_dir)) {

            while (false !== ($entry = readdir($handle))) {

                            if ($entry != "." && $entry != ".." && !is_dir("$path_to_dir$entry")) {
                                    $modified_date = filemtime("$path_to_dir$entry");
                                    if (($timenow - $modified_date)> 86400){
                                            echo "$path_to_dir$entry\n";
                                    }
                                                        }
                                }

                closedir($handle);
}
?>

Upvotes: 0

Mohamed
Mohamed

Reputation: 94

From: https://www.php.net/manual/en/function.filectime.php

You can use the filectime function to get the creation date of your file. The function returns a unix timestamp. You could then subtract that time from the current time to get the amount of seconds ago the file was made.

<?php

# Get the creation date of the file in unix timestamp
$creationDate = filectime("myfile.txt");

# Subtract the creation date from the current time, to get the time difference in seconds
$secondsAgo = time() - $creationDate;

?>

Please note that on most unix systems it will return the last modified date instead of the creation date. This is because on most unix systems the creation date does not exist like on windows.

Upvotes: 4

Zack_dahbi
Zack_dahbi

Reputation: 39

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 good luck :)

Upvotes: -1

Related Questions