| | |||||||
| |||||||
| |||||||
|
|
|
|
|
|
|
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; } ?>
More Date Time Code Articles |
| |
| |