filemtime() returns a Unix timestamp for a file. Check that the file exists first, then format the timestamp for display. This is a file-system timestamp, not automatically a page publication date.
$file = __FILE__;
$modified = filemtime($file);
if ($modified !== false) {
echo date('Y-m-d H:i:s', $modified);
}This example shows the recommended starting pattern. Continue below for variations, explanation and the original practical examples.
$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.
echo date("m/d/Y",filemtime('test.php'));
The above code will display the modified date in month/day/year format. $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.";
}
$file1 = 'file1.txt';
$file2 = 'file2.txt';
if (filemtime($file1) > filemtime($file2)) {
echo "$file1 is newer than $file2.";
} else {
echo "$file2 is newer than $file1.";
}
$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.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| moko | 14-07-2015 |
| thank you, i'm looking this | |