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! |
You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
|
|
|
|
Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations. FREE! Go There Now!
|
|
|
|
Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial of the latest release of IBM Lotus Sametime Standard V8.0. Lotus Sametime Standard V8.0 is a platform for unified communications and collaboration that combines security features with an extensible, open solution including integrated Voice over IP, geographic location awareness, mobile clients, and a robust Business Partner community offering telephony and video integration. FREE! Go There Now!
|
|
|
|
Analysts, architects, and developers who have existing COBOL or PL/I skills and want to extend those skills to deploy new workloads on the mainframe can use the IBM Enterprise Modernization Sandbox for System z to find hands-on walkthroughs of common real world scenarios. The scenarios provide examples of how to rapidly design, create, assemble, test, and deploy high-quality Web, Web services, portal, and SOA applications for IBM CICS, IBM IMS, and IBM WebSphere Application Server. FREE! Go There Now!
|
|
|
|
XML has become a common way of storing business data as flat files and many data server vendors including IBM have provided ways to store this data within relational database systems. Increasingly collections of XML files are accessed like databases using an xQuery and other XML standard mechanisms. Businesses find the need to combine the traditional tabular structured data with XML formatted data. In this webcast, you’ll learn about IBM’s WebSphere Federation Server technology, which provides users with the ability to integrate these two data formats. FREE! Go There Now!
|
|
|
|
Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. FREE! Go There Now!
|
|
|
|
Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. FREE! Go There Now!
|
|
|
|
This paper is about the critical role that a discipline called integrated requirements management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrating, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way. FREE! Go There Now!
|
|
|
|
In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |