w3resource

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

PHP : Exercise-30 with Solution

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

PHP Code Editor:

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

Previous: Write a PHP script to get the names of the functions of a module.
Next: Write a PHP program to swap two variables.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/php-exercises/php-basic-exercise-30.php