Categories: Development

PHP filemtime; file last modified time

 

Getting a file’s modified time with PHP is done by using the filemtime() function, this returns the files last modified time in Unix format.

An example below which also checks if the file exists:

$file = 'info.txt';
if (file_exists($file)) {
    echo filemtime($file);
}

will output: 1589463479.

To get a readable date-time format use the date() function to format as you please.

$file = 'info.txt';
if (file_exists($file)) {
    echo "$file was last modified " . date("Y-m-d H:i:s", filemtime($file));
}

Outputs info.txt was last modified: 2020-05-14 23:37:59.

If file older or newer than

Now checking if a file is within the past 24 hours or older than 24 hours:

$file = 'info.txt';
$diff = time() - filemtime($file);
if ($diff > 86400) {
    echo 'Older than 24 hours';
} else {
    echo 'Newer than 24 hours';
}

As time() returns the current Unix timestamp this can have the file modified time subtracted from it to find the difference, 86400 seconds is 24 hours.

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago