w3resource

PHP Exercises : Get the time of the last modification of the current page


30. Last Modification Time of Current Page

Write a PHP script to get the time of the last modification of the current page.

Sample Solution:

PHP Code:

<?php
// Get the last modification time of the current script using getlastmod()
$last_modified_time = getlastmod();
// Format the last modification time and print it
echo "Last modified: " . date("F d Y H:i:s.", $last_modified_time) . "\n";
?>

Explanation:

  • Get Last Modification Time:
    • The function getlastmod() retrieves the last modification time of the current script in Unix timestamp format.
    • The result is stored in the variable $last_modified_time.
  • Format the Last Modification Time:
    • The date("F d Y H:i:s.", $last_modified_time) function formats the timestamp into a human-readable string, specifying the format as:
      • F: Full textual representation of a month (e.g., January)
      • d: Day of the month (with leading zeros)
      • Y: Four-digit year
      • H: Hour (24-hour format)
      • i: Minutes
      • s: Seconds
  • Output Last Modified Time:
    • The echo statement outputs the formatted last modification time, prefixed by the string "Last modified: ", followed by a newline (\n).

Output:

Last modified: June 26 2017 16:52:16.

The result may varry for your system date and time.

Flowchart:

Flowchart: Get the time of the last modification of the current page

For more Practice: Solve these Related Problems:

  • Write a PHP script to retrieve and display the last modification time of the current file in a formatted style.
  • Write a PHP script to compare the current page's modification time with a stored timestamp and output a status update.
  • Write a PHP script to log the last modified time of the page each time it is accessed.
  • Write a PHP script to convert the last modification time into a human-readable format and display it.


Go to:


PREV : List Module Functions.
NEXT : Swap Variables.

PHP Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.