Miscellaneous Code

  Home arrow Miscellaneous Code arrow PHP Date Validation (including leap ye...

PHP Date Validation (including leap year)
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 16

    Table of Contents:



    The 'GetValidDate()' function ensure that the month, day, and year integers provided are within the allowable month-day combinations for the particular year. This is ideal for validating a date input from a set of drop downs, where a user could select 'February 31, 2003'. Using 'mktime()' will translate those values to 'March 3,2003' which although valid may not be the desired behaviour. This function will determine the proper date without spilling over into the next month and return the timestamp created. A sample use is included. Try selecting 'February 30,2003', 'February 30,2004' and 'November 31,2005' for a clearer picture of what this function does.

    By : wmfwlr

    //** ©William Fowler (wmfwlr@cogeco.ca)
    //** January 26/2004, Version 1.0

    //** Returns: Timestamp
    //** Get the adjusted timestamp associated with the day, month, and year
    //** given. Months and days are indexed starting at one (1). Any date before
    //** January 1, 1970 will be converted to year 1970 (due to 'mktime()'
    //** limitations).

    function GetValidDate($month=1, $day=1, $year=1970)
    //** provide a default value and convert the day of the month given. Day
    //** cannot be less than 1 or greater than 31.

    $day = min(31, max(1, intval($day)));

    //** provide a default value and convert the month number given. Month cannot
    //** be less than 1 or greater than 12.

    $month = min(12, max(1, intval($month)));

    //** provide a default value and convert the day of the year number given.

    $year = max(1970, intval($year));

    //** handle compensating for day runover if the number of days selected is
    //** greater than those allowable for the selected month.

    //** if FEBRUARY the selected day must be corrected and a leap year must be
    //** acounted for as well.
    //** CHANGED JAN 26/2004 - corrected for leap year error.

    case 2 :
    if($day > 28)
    $day = ($year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0)) ? 29 : 28;

    //** only maximum of thirty days in these months.

    case 4 : //** APRIL
    case 6 : //** JUNE
    case 9 : //** SEPTEMBER
    case 11 : //** NOVEMBER

    $day = min(30, $day);
    //** construct the appropriate date timestamp from the parameters provided.

    return mktime(0, 0, 0, $month, $day, $year);

    <form method="post">
    <font face="verdana" size="2">

    <i>Please choose a date:</i><br>

    <select name="month">
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>

    <!-- generate a list of available days in a month, 1 to 31 -->

    <select name="day">
    for($index=1; $index <= 31; $index++)
    echo "<option value='$index'>$index</option>";

    <!-- generate a list of years to select -->

    <select name="year">
    for($index=2000; $index <= 2025; $index++)
    echo "<option value='$index'>$index</option>";

    <input type="submit" name="dodisplay" Value=" Validate "><br><br>

    <!-- if this form was submitted display the date choosen. -->
    echo "<i>You Choose:</i> <font color='blue'>",
    date("F d, Y", GetValidDate($_REQUEST["month"],$_REQUEST["day"],$_REQUEST["year"])),
    "</font> <i>(", "month=", $_REQUEST["month"], " day=", $_REQUEST["day"],
    " year=", $_REQUEST["year"], ")</i>";


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus


    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...

    Developer Shed Affiliates


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