Miscellaneous Code

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

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

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    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.

    switch($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;
    break;

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

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

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

    return mktime(0, 0, 0, $month, $day, $year);
    }
    ?>

    <html>
    <body>
    <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>
    </select>

    <!-- 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>";
    ?>
    </select>

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

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

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

    <!-- if this form was submitted display the date choosen. -->
    <?
    if(isset($_REQUEST["dodisplay"]))
    {
    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>";
    }
    ?>
    </font>

    </form>
    </body>
    </html>

    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
    escort BursaBursa escortescort BursaBursa escortescort BursaAntalya escort bayan Antalya eskort Antalya eskort Antalya escort bayanlar Antalya bayan escort

    MISCELLANEOUS CODE ARTICLES

    - 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-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

    antalya escort escort antalya antalya escortlari antalya escort bayan