Simple function to return the number of days in a time span between 2 given dates.
Arguments are long integers as returned by the standard php function mktime().
Arguments can be generated using mktime(), dateEStime(), or
dateAryEStime(mySQLdate2Ary()).
Example:
A simple illustrator to show how the function is used.
Change comment characters on lines indicated to use text code.
The test code can be used singly or multiply.
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 return the number of days in a time span between 2 given dates.
*
* Arguments are long integers as returned by the standard php function mktime().
* Arguments can be generated using mktime(), dateEStime(), or
* dateAryEStime(mySQLdate2Ary()).
*
* Example:
* A simple illustrator to show how the function is used.
*
* Change comment characters on lines indicated to use text code.
* The test code can be used singly or multiply.
*
* btw, ES|es stands for 'Epoch Seconds'
*
// Change line below from slash-slash to star-slash to use test code
//
$numDays00=daysInSpan(mktime(0,0,0,1,1,2000),mktime(0,0,0,4,1,2000));
$numDays01=daysInSpan(mktime(0,0,0,1,1,2001),mktime(0,0,0,4,1,2001));
print("Using mktime()<br />");
print("The first quarter of 2000 has $numDays00 days (leap year)<br />");
print("The first quarter of 2001 has $numDays01 days (not a leap year)<br /><br />");
/*
*
// Change line below from slash-slash to star-slash to use test code
//
// using DateSpan.php
include_once('DateSpan.php');
$numDays00=daysInSpan(dateEStime(1,1,2000),dateEStime(1,4,2000));
$numDays01=daysInSpan(dateEStime(1,1,2001),dateEStime(1,4,2001));
print("Using dateEStime() from DateSpan.php<br />");
print("The first quarter of 2000 has $numDays00 days (leap year)<br />");
print("The first quarter of 2001 has $numDays01 days (not a leap year)<br /><br />");
/*
*
*/
/*
*
// Change line below from slash-slash to star-slash to use test code
//
// using MySQLdateSpan.php
include_once('MySQLdateSpan.php');
$numDays00=daysInSpan(dateAryEStime(mySQLdate2Ary('2000-1-1')),dateAryEStime(mySQLdate2Ary('2000-4-1')));
$numDays01=daysInSpan(dateAryEStime(mySQLdate2Ary('2001-1-1')),dateAryEStime(mySQLdate2Ary('2001-4-1')));
print("Using mySQLdate2Ary() from MySQLdateSpan.php<br />");
print("The first quarter of 2000 has $numDays00 days (leap year)<br />");
print("The first quarter of 2001 has $numDays01 days (not a leap year)<br />");
/*
*
*/
function daysInSpan($start,$end)
{
$dayTicks=ticksInDay();
return ($end-$start)/$dayTicks;
}
function ticksInDay()
{
$today=getdate();
$yesterday=mktime(0,0,0,$today[mon],$today[mday]-1,$today[year]);
$today=mktime(0,0,0,$today[mon],$today[mday],$today[year]);
return $today-$yesterday;
}
?>
| 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! |
Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
|
|
|
|
Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML. 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!
|
|
|
|
Ken Krugler, co-founder of code search company Krugle, and Laura Merling, vice president of Marketing and Business Development for Krugle, join to talk about the ins and outs of code search and what it means as a new feature for developerWorks users. FREE! Go There Now!
|
|
|
|
Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily. 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!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
|
|
|
|
In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
|
|
|
|
Explore how Rational and WebSphere software enable enterprise documentation in SOA environments. Specifically, a new integration between IBM WebSphere® Business Modeler and IBM Rational® Method Composer software can help technical writers more easily keep enterprise operations manuals in sync with changes that are made to business processes, resulting in more accurate and timely documentation that benefits the entire enterprise. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |