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.