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! |
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!
|
|
|
|
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!
|
|
|
|
As businesses grow increasingly dependent upon Web applications to provide services to customers, employees and partners, these complex applications become more difficult to secure. Although traditional security solutions protect Internet infrastructure layers, they do not guard against HTTP and HTML attacks. Many organizations that conduct security testing still deploy applications that allow attackers to manipulate their logic and wreak havoc on their business. To mitigate this risk, development and delivery teams must address Web application security throughout the lifecycle, addressing the many layers detailed in this paper. 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!
|
|
|
|
Discover how IBM Rational AppScan Standard Edition can help you detext vulnerabilities in your web applications in the Web Application Security eKit. IBM Rational AppScan is a leading suite of automated web application security solutions that scan and test for common Web application vulnerabilities. The new Web Application Security eKit provides you with valuable resources, including white papers, demos, and additional information on the benefits of testing your Web applications. 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!
|
|
|
|
This webcast outlines the best practices that must be instituted to gain the maximum benefit from SOA while maintaining high quality of service. Whether you are deploying new applications or managing and monitoring your existing infrastructure, learn how you can ensure high quality of services with SOA based solutions from IBM. All registrants who attend this live Web Seminar will receive complimentary access to a white paper titled “Maintaining QoS in an SOA Environment”. FREE! Go There Now!
|
|
|
|
Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on November 29 at 1:00 pm ET to participate in an interactive discusssion with Grady Booch around architecture and reuse. Get your questions answered! FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |