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
developerWorks - FREE Tools! |
Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br /> FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development FREE! Go There Now!
|
|
|
|
Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. 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!
|
|
|
|
As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br /> FREE! Go There Now!
|
|
|
|
Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. 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!
|
|
|
|
Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, where he will overview Rational’s new offerings and programs to help customers accelerate software innovation on System z. He will discuss how these solutions help organizations extend their core business processes toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |