OBJECTIVE: given two dates either in Sql format or as Unix timestamps, make their difference and return the time difference both as an aggregated representation (difference expressed
only in seconds,
only in minutes,
only in hours,
only in days) and as a disaggregated representation (difference expressed as how many days hours minutes and seconds have elapsed overall).
function disaggregatedTime($small='', $big=''){
/*validate:*/for($v=0, $a=func_get_args(); $v<sizeof($a); $v++){if(!trim($a[$v])){$a[$v]=time();}; if(strpos(trim($a[$v]), ' ')!==false){$a[$v]=strtotime($a[$v]);}; }
/*init:*/$small=$a[0]; $big=$a[1]; $times=array(86400/*d*/,3600/*h*/, 60/*m*/, 1/*s*/); $times2=array(24,60,60,1); $L=sizeof($times); $output=array();
/*RUN:*/$diff=$big - $small;
for($i=0; $i<$L; $i++){
$computation=$diff/$times[$i]; $output[$i]=floor($computation);
$float=explode('.', $computation); $float=(isset($float[1]))?'0.'.$float[1]:0;
$toreal=round( $float * $times2[$i] , 5 );
switch($i){
case 0: $output['realdays']=$output[$i];
$output['realhours']=(abs($toreal)>=1)?intval($toreal):0; break;
case 1: $output['realminutes']=(abs($toreal)>=1)?intval($toreal):0; break;
case 2: $output['realseconds']=(abs($toreal)>=1)?intval($toreal):0; break;
}
}return $output;/*keep this comment to reuse freely
http://www.fullposter.com/?1 */}Remove colors
INPUTS: both are Strings. They must be dates either represented as
Unix timestamps, or in the following
highly specific format (an
UTC notation with
one whitespace as
designator):
yyyy-mm-dd hh:mm:ss
The first argument ought to be the
earlier ("smaller") date, although no validation for this requirement is performed.
If no inputs are passed, they will
both default to the
current timestamp.
RETURNS: an
associative array whose key/values pairs (keys are numbers and strings both, values are always numbers) are as follows:
0 = aggregated days
'realdays' = disaggregated days
1 = aggregated hours
'realhours' = disaggregated hours
2 = aggregated minutes
'realminutes' = disaggregated minutes
3 = aggregated seconds
'realseconds' = disaggregated seconds
CAVEATS: given the presence of leap years and of different amounts of days in a month, the function does not add representations in months and years: however, were it to be needed, it ought to be worked out considering the starting offset date and the returned
aggregated days. Or, for such purpose you may use instead:
Php Date: Full Disaggregated Date Difference Accounting Timezones And Daylight.
The functions performs a
rounding to avoid the notorious
accuracy problem: this rounding allows
5 floating digits, and were you to need to change that amount the concerned line is:
$toreal=round( $float * $times2[$i] , 5 );
Remove colors
If you pass as first argument a date that is not the earlier (therefore
inverting the way the function is designed to operate), the returned value of the
day (if the difference spans throughout at least one day) will be
negative but the others will
still be
positive so you cannot rely on them to determine whether this was a case of negative difference.
Remember that the returned values may amount also to zero, in case you want to skip them from print.
Example of use:
$foo=disaggregatedTime('2005-03-23 22:05:30', '2005-03-24 20:04:29');
echo "Between <strong>2005-03-23 22:05:30</strong> and <strong>2005-03-24 20:04:29</strong> the difference is: {$foo['realdays']} days, {$foo['realhours']} hours, {$foo['realminutes']} minutes, {$foo['realseconds']} seconds";Remove colorsSuch example would print:
Between 2005-03-23 22:05:30 and 2005-03-24 20:04:29 the difference is: 0 days, 21 hours, 58 minutes, 59 seconds