Programming Basics

  Home arrow Programming Basics arrow Getting Modified Versions and Correct ...
PROGRAMMING BASICS

Getting Modified Versions and Correct Dates in PHP
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0
    2011-12-28

    Table of Contents:
  • Getting Modified Versions and Correct Dates in PHP
  • Calculating the Date X Days from the Present Date

  •  
     

    SEARCH CODEWALKERS

    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().

    getlastmod()

    int getlastmod()

    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:

    There are 28 days in February, 2006.

    More Programming Basics Articles
    More By Apress Publishing

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - The Transliteration Operator in Perl
    - Perl String Processing Functions
    - Perl String Processing
    - Control Flow Constructs: Loops Conclusion
    - Loop Control Constructs
    - Control Flow Constructs: the For and Foreach...
    - Loops and Control Flow Constructs
    - Expression Modifiers for Perl Control Flow C...
    - Logical Operators and Control Flow Constructs
    - Comparing Strings with Control Flow Construc...
    - Perl Operators and Control Flow Constructs
    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP

    Developer Shed Affiliates

     



    © 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap