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…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago