Programming Basics

  Home arrow Programming Basics arrow Validating and Manipulating Dates with...
PROGRAMMING BASICS

Validating and Manipulating Dates with PHP
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0
    2012-01-25

    Table of Contents:
  • Validating and Manipulating Dates with PHP
  • Manipulation Methods

  •  
     

    SEARCH CODEWALKERS

    Validating and Manipulating Dates with PHP


    (Page 1 of 2 )

    In this seventh part of an eight-part article series on handling dates and time with PHP, you'll learn how to use the date function's validators, and how to easily manipulate dates. 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).

    Validators

    Date offers a method for determining whether the date falls on a leap year and a method for validating the dateís correctness. Both of those methods are introduced in this section.

    isLeap()

    boolean isLeap()

    TheisLeap()method returnsTRUE if the year represented by the date object is a leap year, andFALSEotherwise. The following script usesisLeap()in conjunction with a ternary operator to inform the user whether a given year is a leap year:

    $year = 2005;
    $date = new Date(date("j"),date("n"),$year); echo "$year is ". ($date->isLeap() == 1 ? "" : "not"). " a leap year.";

    This produces the following output:

    --------------------------------------------2005 is not a leap year.
    --------------------------------------------

    isValid()

    boolean isValid()

    TheisValid()method returnsTRUEif the date represented by the date object is valid, andFALSEotherwise. Because this method canít be called statically, and itís impossible to set an invalid date using the constructor of any of the mutators, it isnít presently apparent whyisValid()exists.

    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