w3resource

PHP Exercises : Get last modified information of a file

PHP : Exercise-15 with Solution

Write a PHP script to get last modified information of a file.

Sample filename : php-basic-exercises.php

Sample Solution: -

PHP Code:

<?php
$current_file_name = basename($_SERVER['PHP_SELF']);
$file_last_modified = filemtime($current_file_name); 
echo "Last modified " . date("l, dS F, Y, h:ia", $file_last_modified)."\n";
?>

Sample Output:

Last modified Monday, 26th June, 2017, 02:06pm

Flowchart:

Flowchart: Get last modified information of a file

Note: The result may vary for your system date and time.

basename() function: The basename(path,suffix) function is used to get the filename from a path.

filemtime() function: The filemtime(filename) function is used to get the last time the file content was modified.

PHP Code Editor:

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

Previous: Write a PHP script to display source code of a webpage (e.g. "http://www.example.com/").
Next: Write a PHP script to count number of lines in a file.

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.

PHP: Tips of the Day

Mutates the original array to filter out the values specified

Example:

<?php
function tips_pull(&$items, ...$params)
{
  $items = array_values(array_diff($items, $params));
  return $items;
}

$items = ['x', 'y', 'z', 'x', 'y', 'z'];
print_r(tips_pull($items, 'y', 'z'));
?>

Output:

Array
(
    [0] => x
    [1] => x
)