Get the Time of the Last Modification of the Current Page in PHP

In PHP, you can retrieve the time of the last modification of the current page using the filemtime() function.

This is useful when you want to display or log the last modification time of the page or for various other purposes like caching and version tracking.

In this article, we'll explore how to get the time of the last modification of the current page in PHP.

Using filemtime() function

The filemtime() function in PHP is used to retrieve the last modification time of a file.

To get the last modification time of the current PHP page, you can use $_SERVER['SCRIPT_FILENAME'] to access the current page's filename.

Here's an example:

$filePath = $_SERVER['SCRIPT_FILENAME'];
$lastModificationTime = filemtime($filePath);
$lastModificationDateTime = date("Y-m-d H:i:s", $lastModificationTime);

echo "Last Modification Time: $lastModificationDateTime";

In this code:

When you run this code on your PHP page, it will display the last modification time in the "Y-m-d H:i:s" format.

Example Output

For example, if you run this code on a PHP page that was last modified on October 1, 2023, at 14:30:00, the output would be:

Last Modification Time: 2023-10-01 14:30:00

This output represents the last modification time of the current page.

Note

Keep in mind that the filemtime() function provides the last modification time based on the server's filesystem. If the file is cached or the server doesn't accurately track modifications, the result may not always reflect the last actual modification time of the PHP script.


Conclusion

Retrieving the last modification time of the current page in PHP is a valuable task for various purposes, such as tracking changes, displaying version information, or optimizing caching strategies.

Using the filemtime() function in combination with $_SERVER['SCRIPT_FILENAME'], you can easily obtain and display the last modification time of the current PHP page.