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! |
David Barnes, Lead Evangelist for IBM Emerging Internet Technologies 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!
|
|
|
|
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!
|
|
|
|
The XO laptop (of the One-Laptop-Per-Child initiative) is an inexpensive laptop project intended to help educate children around the world. The XO laptop includes many innovations, such as a novel, inexpensive, and durable hardware design and the use of GNU/Linux as the underlying operating system. The XO also includes an application environment written in Python with a human interface called Sugar, accessible to everyone (including kids). Explore the Sugar APIs and learn how to develop and debug a graphical activity in Sugar using Python. FREE! Go There Now!
|
|
|
|
Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects. FREE! Go There Now!
|
|
|
|
To create, test, and deploy a Web-based application or Web service rapidly, you need a proven relational database, a standards-compliant Web application server, and a flexible IDE. Ideally, all these software packages are production-tested, simple to obtain, easy to use, and well integrated with one another. This tutorial shows you how to use IBM-backed open source and free software to kick-start your Java Web-based application development. You'll learn exactly where to download such components, install them, and get them working for you today. FREE! Go There Now!
|
|
|
|
Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms. FREE! Go There Now!
|
|
|
|
Join this webcast to discover the key requirements for successful change and release management. Learn how to extend your .NET environment to improve productivity and collaboration, and address core problems afflicting team development. In this webcast, we’ll review typical challenges faced by customers and how to resolve them with the IBM Rational Change and Release Management solution, including Rational ClearCase, Rational ClearQuest and Rational Build Forge. Replay is available for 9 months. 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!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |