Tutorials > PHP/MySQL > PHP: Last Modified

Details
Posted by: Sarah
Date: August 15 2008
2 Responses
Category: PHP/MySQL
Required: PHP Support
PHP: Last Modified

If you wish to display the date and time a file was last modified or changed, this simple PHP code snippet will do the trick. Below, I describe two different ways of displaying the last modified functions each suitable for a different setting - whether you want to show the last modified date of an external file, or the file itself.

Current File

To display the last modified date of example.php add the following code where you wish for the date to appear.

Last updated on <?php echo" " . date ("F d Y", getlastmod()); ?>

F - This displays the Month's full name.
d - This displays the date in 2 digit numbers.
Y - This displays the full year in numbers.

F d Y is the date format which will be displayed. For further customizations, you can view the Date Format chart further down this page. Also, you can add text before and after the <?php and ?> tags. As well, add in your own text within "F d Y" if you wish to add text in between. Example: "F/d/Y"

External File

If you want to add the last modified date of a file that is not your current file, then you can use the following code.

<?php
// Change to the name of the file
$last_modified = filemtime("thisfile.php");

// Display the results
// eg. Last modified Thursday, 14th August, 2008 @ 11:52pm
print "Last modified " . date("l, dS F, Y @ h:ia", $last_modified);
?>

Make the changes stated above. Change thisfule.php to the name of the external file you wish to display the last modified date. Remember, you must use the absolute path (/home/username/public_html/thisfile.php) and not the URL (http://domain.com/thisfile.php) in order for the the code to work properly.

- Last modified Change the bolded text before the date if you wish to display a different lablel, like "Changed" or "Last updated on".
- l, dS F, Y @ h:ia This will set the format of the date displayed. Currently, the format will look like Thursday, 14th August, 2008 @ 11:52pm However, you can change the characters around by selecting your own formatting characters seen below.

Date Formats

The following chart provides you with more characters to modify the format of your date. Please note that capitalizing a latter makes a difference. Pay attention to CAPSLOCK if you have it turned on.

a = am or pm
A = AM or PM
d = day in numbers (01 to 31)
j = Day in numbers (1 to 31)
D = day in abbreviations (Mon, Tue, …)
l = Day (Monday, Tuesday, …) (this is a lowercase L)
F = Month (January, February, …)
M = Month in abbreviations (Jan, Feb, …)
m = Month (01 to 12)
n = Month (1 to 12)
g = Hour (1 to 12)
G = Hour (1 to 24)
h = Hour (01 to 12)
H = Hour (01 to 24)
i = Minutes (00 to 59)
s = Seconds (00 to 59)
y = Year (2 digits) (e.g. 06, 07, etc.)
Y = Year (4 digits) (e.g. 2006, 2007, etc.)
S = Suffix (st, nd, rd, …) (e.g. 1st)
t = Number of days in current month (28, 29, 30, 31)
w = Day of the week (0 for sunday to 6 for saturday)
z = Day of year (0 to 365/364)

Responses

1 Cassandra Says:

thank u


2 Simon Massey Says:

cdtxc764f5v44536


 

Leave A Reply

Name (required)
Email (required)
Website (optional)

Note: *If you need to post html or php code - transform your codes first at SimpleCode before posting. // All posts are moderated, so please keep posting codes to a minimum.


Layout © Sarah | Resources: 1 2 | Powered by Wordpress