MoonPoint Support Logo

 

Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
November
Sun Mon Tue Wed Thu Fri Sat
     
5
   
2006
Months
Nov


Sun, Nov 05, 2006 10:55 pm

Displaying the Modification Time for a Webpage with PHP

You can display the last time a webpage was modified by including the following PHP code on a webpage:


<?php

$thisfile = pathinfo($_SERVER['PHP_SELF']);

echo "Last modified: ".date("l jS F Y g:ia",
filemtime($thisfile["basename"]));

?>

Note: your webpage must have a .php extension rather than .htm or .html and your webserver must provide PHP support in order for the code to work.

The above code would display the date and time the webpage was modified in the format below:

Last modified: Sunday 5th November 2006 8:57pm

The options to the PHP date function above are encluded in parentheses. Within the parentheses the first argument is the date format to be used, which is followed by a comma and then the time value to be formatted. In this case the time value to be formatted is the file modification time, filemtime of the webpage.

The lowercase "L" will display the day of the week, e.g. "Sunday". The lowercase "j" displays the day of the month without leading zeros, e.g. "5". Putting the "S" immediately after it displays two characters for the English ordinal suffix for the day of the month. In the case above it causes the "th" to be put after the "5". The "F" displays the full month name, e.g. "November" and the "Y" displays the year as 4 digits, e.g. "2006". The "g" displays the hour in 12 hour format without leading zeros, e.g. "8" in the above case. It is followed by a colon and then the "i" displays the minutes with leading zeros, e.g. "07" or in this case "57". The "a" displays a lowercase "am" or "pm" as the case may be.

The characters you can use to control the display of the date are as follows:

a 'am' or 'pm'
A 'AM' or 'PM'
B Swatch Internet time
d day of the month, 2 digits with leading zeros; i.e. '01' to '31'
D day of the week, textual, 3 letters; i.e. 'Fri'
F month, textual, long; i.e. 'January'
g hour, 12-hour format without leading zeros; i.e. '1' to '12'
G hour, 24-hour format without leading zeros; i.e. '0' to '23'
h hour, 12-hour format; i.e. '01' to '12'
H hour, 24-hour format; i.e. '00' to '23'
i minutes; i.e. '00' to '59'
I (capital i) '1' if Daylight Savings Time, '0' otherwise.
j day of the month without leading zeros; i.e. '1' to '31'
l (lowercase 'L') day of the week, textual, long; i.e. 'Friday'
L boolean for whether it is a leap year; i.e. '0' or '1'
m month; i.e. '01' to '12'
M month, textual, 3 letters; i.e. 'Jan'
n month without leading zeros; i.e. '1' to '12'
r RFC 822 formatted date; i.e. 'Thu, 21 Dec 2000 16:01:07 +0200' (added in PHP 4.0.4)
s seconds; i.e. '00' to '59'
S English ordinal suffix, textual, 2 characters; i.e. 'th', 'nd'
t number of days in the given month; i.e. '28' to '31'
T Timezone setting of this machine; i.e. 'MDT'
U seconds since the epoch
w day of the week, numeric, i.e. '0' (Sunday) to '6' (Saturday)
Y year, 4 digits; i.e. '1999'
y year, 2 digits; i.e. '99'
z day of the year; i.e. '0' to '365'
Z timezone offset in seconds (i.e. '-43200' to '43200'). The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.

For another example, using the following code woulld display the same date as above as Sunday November 5, 2006 8:57 PM instead.


<?php

$thisfile = pathinfo($_SERVER['PHP_SELF']);

echo "Last modified: ".date("l F j, Y g:i A",
filemtime($thisfile["basename"]));

?>

You can put the code in a PHP file that can be included in every webpage, so that if you decide to change the format of the displayed date, you don't have to modify every web page that you have on your website. For instance, I include a "footer.php" file in webpages using incfile.

References:

  1. php displaying last modification time
    thescripts developer community
    July 17, 2005
  2. PHP:date - Manual
    The PHP Group
    September 28, 2006
  3. PHP Date()
    W3Schools
  4. Date Format php for month day year and time formatting
    Plus2net
  5. Including Files in a Web Page with PHP
    MoonPoint Support
    February 8, 2006

[/languages/php] permanent link

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo