filemtime():Getting the last updated time of the file

$file = $_SERVER["SCRIPT_NAME"];
    $break = Explode('/', $file);
    $pfile = $break[count($break) - 1]; 
//echo $pfile;
echo "This file was last modified on: " .date("d/m/Y",filemtime($pfile));
We can get the last updated date of any file by using filemtime() function in PHP. This function returns date in UNIX Timestamp format and we can convert it to our requirement by using date function.

This function filemtime() uses the server file system so it works for local systems only, we can't use this function to get the modified time of any remote file system.

Here is the code to get the last modified date of any file. We are checking for a existing file ( test.php)
echo date("m/d/Y",filemtime('test.php'));
The above code will display the modified date in month/day/year format.
Note that we have used date function to convert the Unix Timestamp time returned by filemtime() function.

Example: Checking if a File is Updated Recently

$file = 'example.txt';
if (file_exists($file) && time() - filemtime($file) < 86400) {
    echo "File was updated in the last 24 hours.";
} else {
    echo "File was not updated recently.";
}

Example: Comparing Two Files' Modification Times

$file1 = 'file1.txt';
$file2 = 'file2.txt';
if (filemtime($file1) > filemtime($file2)) {
    echo "$file1 is newer than $file2.";
} else {
    echo "$file2 is newer than $file1.";
}

List of file name with modification date & time of a directory

$dir = 'E:\\dirname\\sub_dir'; 

if (is_dir($dir)) {
    echo "<table border='1'>
            <tr><th>File Name</th><th>Last Modified Time</th></tr>";
    
    $files = scandir($dir);
    foreach ($files as $file) {
        if ($file != '.' && $file != '..' && is_file("$dir/$file")) {
            //echo "<tr><td>$file</td><td>" . date("F d Y H:i:s", filemtime("$dir/$file")) . "</td></tr>";
	echo "<tr><td>$file</td><td>" . gmdate("F d Y H:i:s", filemtime("$dir/$file")) . "</td></tr>"; // UTC time

        }
    }    
    echo "</table>";
} else {
    echo "Directory does not exist.";}
To display the above list in the order of file modification date and time we can use usort().
$dir = 'E:\\dirname\\sub_dir'; 
if (is_dir($dir)) {
    $files = scandir($dir);
    $fileData = [];

    foreach ($files as $file) {
        if ($file != '.' && $file != '..' && is_file("$dir/$file")) {
            $fileData[] = ['file' => $file, 'mtime' => filemtime("$dir/$file")];
        }
    }

    // Sort by modification time (most recent first)
    usort($fileData, function($a, $b) {
        return $b['mtime'] - $a['mtime'];
    });

    echo "<table border='1'>
            <tr><th>File Name</th><th>Last Modified Time</th></tr>";

    foreach ($fileData as $file) {
        echo "<tr><td>{$file['file']}</td><td>" . date("F d Y H:i:s", $file['mtime']) . "</td></tr>";
    }

    echo "</table>";
} else {
    echo "Directory does not exist.";
}
You may see a difference in time due to time zone difference between your server's local time and your current time zone. You can adjust the timezone using:
date_default_timezone_set('America/New_York'); // Set your correct timezone
Place this at the beginning of your script to ensure the displayed times reflect your desired timezone. You can replace 'America/New_York' with your local timezone.

These examples offer practical use cases for `filemtime()` in managing and comparing file modification times.
File Deleting File Checking if file exists before deleting
PHP
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    moko

    14-07-2015

    thank you, i'm looking this




    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer