PLEASE NOTE:
This is code to generate a selection entry form for a future date, not a
function to validate dates.
futureDateFormEntry():
Quick, easy addition of future date fields to HTML code using PHP.
Handy for requesting expiration dates, dates for delivery, dates for
appointments, or any web application that requires obtaining a future
date from the user.
validFutureDate():
Checks that entry is indeed a valid future date.
Code introduction comment contains a fully functional example for illustrative/testing purposes.
By : dsilvia
<?php
/*
* (c) 2006, D.E. Silvia, All rights reserved.
* This code is available for use for non-commercial purposes.
* Free to distribute as long as this copyright information remains intact.
* No modification is authorized. Please, refer bugs/enhancements to
* dsilvia@mchsi.com
*
*/
/*
* futureDateFormEntry():
* Quick, easy addition of future date fields to HTML code using PHP.
* Handy for requesting expiration dates, dates for delivery, dates for
* appointments, or any web application that requires obtaining a future
* date from the user.
*
* Function requires only 3 arguments if you are happy with the defaults:
* The 3 required arguments are a mechanism for preserving the user's valid
* input from previous edits of the form (re-entrant form code). They may
* be NULL, in which case the default selection parameters apply.
*
* Example:
<?php
include('FutureDateFormEntry.php');
$userName=$_REQUEST[yourName];
$enteredDay=$_REQUEST[expiryDay];
$enteredMonth=$_REQUEST[expiryMonth];
$enteredYear=$_REQUEST[expiryYear];
$goodEntry=true;
$entryMade=($enteredDay && $enteredMonth && $enteredYear);
if($entryMade)
$goodEntry=validFutureDate($enteredDay,$enteredMonth,$enteredYear);
?>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
if($entryMade && !$userName)
{
print("Name field is empty<br />");
}
if(!$goodEntry)
{
print($enteredDay."/".$enteredMonth."/".$enteredYear." is not in the future<br />");
}
elseif($entryMade)
{
print($enteredDay."/".$enteredMonth."/".$enteredYear." is a valid future date<br />");
}
?>
<form method="post" action="test.php">
<input name="yourName" type="text" value="<?php print($userName); ?>"> Your Name<br />
<?php print(futureDateFormEntry($enteredDay,$enteredMonth,$enteredYear)); ?>
Give me some date in the Future<br />
<input name="gotEntry" type="submit" value="Submit">
</form>
</body>
</html>
*
* $defaultDay: Specific day (may be NULL)
* $defaultMonth: Specific month (may be NULL)
* $defaultYear: Specific year (may be NULL)
* $order='dmy': Euro convention of day:month:year
* $alignment='left': standard; can be any "<td align=" value
* default selections:
* $plusDays=0: assumes same day of month as today's or maximum day
* for the given month; number of days you want added
* to today's month day
* $plusMonths=1: one month in the future; number of months you want
* added to today's month
* $plusYears=0: current year or next year if current month is 12 [Dec];
* number of years you want added to today's year
* $futureYears=1: number of years in the future to include in selection
* beyond the default selected year.
* <select name="input-name":
* $dayName='expiryDay'
* $monthName='expiryMonth'
* $yearName='expiryYear'
*/
function futureDateFormEntry($defaultDay,$defaultMonth,$defaultYear,
$order='dmy',
$plusDays=0,
$plusMonths=1,
$plusYears=0,
$futureYears=1,
$dayName='expiryDay',
$monthName='expiryMonth',
$yearName='expiryYear'
)
{
if($plusYears > $futureYears) $futureYears=$plusYears;
/*
* Yes, I know, this array doesn't account for leap years! So? Is that really a problem?
* The user can always select the 29th of February anyway!
*/
$lastDay=array(0,31,28,31,30,31,30,31,31,30,31,30,31);
$today=getdate();
$tDay=$today[mday];
$tMon=$today[mon];
$tYear=$today[year];
if($tMon == 12)
{
$theYear++;
}
if($defaultDay && $defaultMonth && $defaultYear)
{
if(!validFutureDate($defaultDay,$defaultMonth,$defaultYear))
{
unset($defaultDay,$defaultMonth,$defaultYear);
}
}
$theDay=$tDay+$plusDays;
$theMonth=$tMon+$plusMonths;
if($theMonth > 12) $theMonth-=12;
$theYear=$tYear+$plusYears;
if($theDay > $lastDay[$theMonth]) $theDay=$lastDay[$theMonth];
if($defaultDay) $theDay=$defaultDay;
if($defaultMonth) $theMonth=$defaultMonth;
if($defaultYear) $theYear=$defaultYear;
$mSelect=array_pad(array(''),13,'');
$mSelect[$theMonth]='selected';
$dSelect=array_pad(array(''),32,'');
$dSelect[$theDay]='selected';
$entryStr='';
for($i=0; $i < 3; $i++)
{
if($order[$i] == "m")
$entryStr.='
<select name="'.$monthName.'">
<option '.$mSelect[1].' value="1">1 [Jan]</option><option '.$mSelect[2].' value="2">2 [Feb]</option>
<option '.$mSelect[3].' value="3">3 [Mar]</option><option '.$mSelect[4].' value="4">4 [Apr]</option>
<option '.$mSelect[5].' value="5">5 [May]</option><option '.$mSelect[6].' value="6">6 [Jun]</option>
<option '.$mSelect[7].' value="7">7 [Jul]</option><option '.$mSelect[8].' value="8">8 [Aug]</option>
<option '.$mSelect[9].' value="9">9 [Sep]</option><option '.$mSelect[10].' value="10">10 [Oct]</option>
<option '.$mSelect[11].' value="11">11 [Nov]</option><option '.$mSelect[12].' value="12">12 [Dec]</option>
</select>';
if($order[$i] == "d")
$entryStr.='
<select name="'.$dayName.'">
<option '.$dSelect[1].' value="1"> 1</option><option '.$dSelect[2].' value="2"> 2</option>
<option '.$dSelect[3].' value="3"> 3</option><option '.$dSelect[4].' value="4"> 4</option>
<option '.$dSelect[5].' value="5"> 5</option><option '.$dSelect[6].' value="6"> 6</option>
<option '.$dSelect[7].' value="7"> 7</option><option '.$dSelect[8].' value="8"> 8</option>
<option '.$dSelect[9].' value="9"> 9</option><option '.$dSelect[10].' value="10">10</option>
<option '.$dSelect[11].' value="11">11</option><option '.$dSelect[12].' value="12">12</option>
<option '.$dSelect[13].' value="13">13</option><option '.$dSelect[14].' value="14">14</option>
<option '.$dSelect[15].' value="15">15</option><option '.$dSelect[16].' value="16">16</option>
<option '.$dSelect[17].' value="17">17</option><option '.$dSelect[18].' value="18">18</option>
<option '.$dSelect[19].' value="19">19</option><option '.$dSelect[20].' value="20">20</option>
<option '.$dSelect[21].' value="21">21</option><option '.$dSelect[22].' value="22">22</option>
<option '.$dSelect[23].' value="23">23</option><option '.$dSelect[24].' value="24">24</option>
<option '.$dSelect[25].' value="25">25</option><option '.$dSelect[26].' value="26">26</option>
<option '.$dSelect[27].' value="27">27</option><option '.$dSelect[28].' value="28">28</option>
<option '.$dSelect[29].' value="29">29</option><option '.$dSelect[30].' value="30">30</option>
<option '.$dSelect[31].' value="31">31</option>
</select>';
if($order[$i] == "y")
{
$entryStr.='
<select name="'.$yearName.'">';
for($j=$tYear; $j < $theYear; $j++)
{
$entryStr.='
<option value="'.$j.'">'.$j.'</option>';
}
$entryStr.='
<option selected value="'.$theYear.'">'.$theYear.'</option>';
for($j=1; $j <= $futureYears; $j++)
{
$nYear=$theYear+$j;
$entryStr.='
<option value="'.$nYear.'">'.$nYear.'</option>';
}
$entryStr.='
</select>';
}
}
return $entryStr;
}
/*
* argument values are assumed to be returns from a form entry generated
* by futureDateFormEntry(). As such, the year can never be in the past.
*/
function validFutureDate($theDay,$theMonth,$theYear)
{
$today=getdate();
$tDay=$today[mday];
$tMon=$today[mon];
$tYear=$today[year];
$cmpMon=$theMonth;
$goodDate=true;
if($theYear > $tYear)
{
$cmpMon+=12;
}
/* is it in the 'long' past? */
if($cmpMon < $tMon)
{
$goodDate=false;
}
/* is it recent past or today? */
elseif($theDay <= $tDay && $cmpMon == $tMon && $theYear == $tYear)
{
$goodDate=false;
}
return $goodDate;
}
?>
| 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 Date Time Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
<a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download the latest trial version of IBM Data Studio V1.1 at no cost. IBM Data Studio is a comprehensive data management solution that helps you effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life cycle utilizing a consistent and integrated user interface. Unlike other client-side data management solutions that focus on only one aspect of the application lifecycle or database administration, Data Studio complements the Rational Software Delivery platform, providing unparalleled flexibility for a heterogeneous data server environment across platforms. FREE! Go There Now!
|
|
|
|
Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code. FREE! Go There Now!
|
|
|
|
This tutorial presents an innovative use of the well-known Really Simple Syndication (RSS) format's associative properties to emulate the functionality of a simple relational database. It demonstrates using RSS channels to store contact information and meeting information -- much as a personal address book and calendar does. It uses RSS elements and attributes such as items and guids to create a neural-network-like mesh of related data. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered! FREE! Go There Now!
|
|
|
|
Investigate the effects of field-level caching in dynamic array access, in part 5 of the UniVerse performance series. FREE! Go There Now!
|
|
|
|
Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications. FREE! Go There Now!
|
|
|
|
The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
|
|
|
|
User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |