Getting Modified Versions and Correct Dates in PHP
(Page 1 of 2 )
In this third part of an eight-part article on working with date and time functions in PHP, you'll learn how to get the last modified version of a document, calculate dates, and more. This article is excerpted from chapter 12 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).
Displaying the Web Pageís Most Recent Modification Date
Barely a decade old, the Web is already starting to look like a packratís office. Documents are strewn everywhere, many of which are old, outdated, and often downright irrelevant. One of the commonplace strategies for helping the visitor determine the documentís validity involves adding a timestamp to the page. Of course, doing so manually will only invite errors, as the page administrator will eventually forget to update the timestamp. However, itís possible to automate this process using date()andgetlastmod(). You already knowdate(), so this opportunity is taken to introducegetlastmod().
Thegetlastmod()function returns the value of the pageís Last-Modified header, orFALSEin the case of an error. If you use it in conjunction withdate(), providing information regarding the pageís last modification time and date is trivial:
$lastmod = date("F d, Y h:i:sa", getlastmod()); echo "Page last modified on $lastmod";
This returns output similar to the following:
Page last modified on April 26, 2005 07:59:34pm
Determining the Number Days in the Current Month
To determine the number of days found in the present month, use the date()functionístparameter. Consider the following code:
printf("There are %d days in %s.", date("t"), date("F"));
If this was executed in April, the following result would be output:
There are 30 days in April.
Determining the Number of Days in Any Given Month
Sometimes you might want to determine the number of days in some month other than the present month. Thedate()function alone wonít work because it requires a timestamp, and you might only have a month and year available. However, themktime()function can be used in conjunction withdate()to produce the desired result. Suppose you want to determine the number of days found in February of 2006:
$lastday = mktime(0, 0, 0, 3, 0, 2006); printf("There are %d days in February, 2006.", date("t",$lastday));
Executing this snippet produces the following output: