Simple function to check if a given date falls within a given time span.
Code contains a fully functional illustrative example. Just change the commenting as instructed in the file to run the example. It's a matter of changing a single character (from '/' to '*').
Useful for determining, say, if a date a user has entered falls during your vacation?;)
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
*
*/
/*
* Simple function to check if a given date falls within a given time span.
*
* Arguments are arrays of the form array(dayNum,monthNum,yearNum) as recognized
* by the standard php function mktime().
*
* Example:
* A simple illustrator to show how the functions are used.
*
* btw, ES|es stands for 'Epoch Seconds'
*
replace first character, single line comment character slash ('/'),
with multiline comment character star ('*')
in the line below to use the test code
//?>
<?php
include_once('DateSpan.php');
$startDate=array(1,1,2006);
$endDate=array(1,1,2007);
$find[0]=array(1,8,2007);
$find[1]=array(4,8,2006);
$esTime1=dateAryEStime($startDate);
$esTime2=dateAryEStime($endDate);
$date1=date("M-d-Y",$esTime1);
$date2=date("M-d-Y",$esTime2);
for($i=0; $i < 2; $i++)
{
$date3=date("M-d-Y",dateAryEStime($find[$i]));
if(dateBetween($startDate,$endDate,$find[$i]))
{
print("$date3 falls between $date1 and $date2 (inclusive)<br />");
}
else
{
print("$date3 falls outside $date1 and $date2 (inclusive)<br />");
}
}
?>
<?php /*
*
*/
function dateBetween($start,$end,$theDate)
{
$startES=dateAryEStime($start);
$endES=dateAryEStime($end);
$findES=dateAryEStime($theDate);
return ($startES <= $findES && $findES <= $endES);
}
function dateAryEStime($theDate)
{
return dateEStime($theDate[0],$theDate[1],$theDate[2]);
}
function dateEStime($theDay,$theMonth,$theYear)
{
return mktime(0,0,0,$theMonth,$theDay,$theYear);
}
?>
| 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! |
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 webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance. FREE! Go There Now!
|
|
|
|
Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan. 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!
|
|
|
|
CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP. FREE! Go There Now!
|
|
|
|
IBM DB2 9.5 provides new options for tighter security, and allows for more granularity and flexibility in administration of the database. This tutorial is the first of two tutorials that cover roles and trusted contexts. Follow the exercises in this tutorial, and learn how to take advantage of the new DB2 feature roles in combination with other essential e-business technologies such as Web services, Web application server, and DB2 database server. FREE! Go There Now!
|
|
|
|
The combination of Eclipse, DB2 Express-C 9.5, and WebSphere Application Server Community Edition 2.0 -- all free to download, use, and deploy -- is an excellent from-prototype-to-production suite for all of your Java and Java enterprise development needs. What might not be obvious is the relative ease with which you can use these proven tools to create, test, and deploy cutting-edge, lightweight applications as well. This tutorial guides you through the development of a small human-resources application, first using conventional JavaServer Pages (JSP) based technology, and then migrating it to a highly interactive solution using Ajax. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! 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!
|
|
|
|
IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |