<?
    
function counttime($start$stop false$display "combined") {
        
# version 1.3
        # Author: Jonas Eklundh Communication AB
        
        
require_once ("adodb_time.php");
        if (!
$stop$stop adodb_date("Y-m-d");
        
$sec["seconds"] = 1;
        
$sec["minutes"] = $sec["seconds"]*60;
        
$sec["hours"] = $sec["minutes"]*60;
        
$sec["days"] = $sec["hours"]*24;
        
$sec["weeks"] = $sec["days"]*7;
        
$sec["months"] = $sec["days"]*28# 28 so "feb 01-feb 28" is considered one month
        
$sec["years"] = $sec["days"]*365;; # 365 includes leap years
        
$sec array_reverse($sec);
        
        
# make sure that $from is always the earliest date
        
$from $stop $start $start $stop;
        
$to $stop $start $stop $start;
        
        
# extract the parts of each date, for adodb_mktime
        
$fp split("[ :-]"$from);
        
$tp split("[ :-]"$to);
        
        
# get time
        
$target adodb_mktime($tp[3], $tp[4], $tp[5], $tp[1], $tp[2], $tp[0]);
        
$source adodb_mktime($fp[3], $fp[4], $fp[5], $fp[1], $fp[2], $fp[0]);
        
        
# the difference, in seconds, between the two dates
        
$diff $target-$source;
        if (
$display == "combined") {
            if (
$diff >= $sec["years"]) {
                
# Ok, the time is longer than at least one year
                
for ($y adodb_date("Y"$source);$y <= adodb_date("Y"$target);$y++) {
                    
$day adodb_date("m-d"$source) == "02-29" 28 $fp[2]; # considering feb 29, since we can't use it consistently
                    
$comptime adodb_mktime($fp[3], $fp[4], $fp[5], $fp[1], $day$y); #stepping a full year into the future. i.e. same date
                    
if ($comptime $source && $comptime <= $target) {
                        
# if comptime is within the range, a full year has elapsed
                        
$result["years"]++; # so we add a year to the result
                        
$diff $target-$comptime# and remove the time we're at from the diff
                    
}
                }
            }
            if (
$diff >= $sec["months"]) {
                
# Ok, the time left is at least one month long
                
$starttime $target-$diff# This is the second we're starting on
                
for ($i $starttime;$i <= $target;$i+= $sec["days"]) {
                    
$comp_day adodb_date("d"$source); # This is the day of month we're comparing against
                    
if ($comp_day adodb_date("t"$i)) $comp_day adodb_date("t"$i); #making sure we don't compare 31 jan to 31 feb
                    
if (date("d"$i) == $comp_day && $i != $starttime) {
                        
# It's the same day of month, so one month has passed
                        # So we subtract the aggregated time from the diff
                        
$hour $fp[3] ? $fp[3] : "00"# if hour is specified, we need to use that as well
                        
$diff $target-strtotime(adodb_date("Y-m-$comp_day $hour:i"$i)); # setting diff to the amount of seconds so far
                        
$result["months"]++; # and add one moth to the result.
                        
                    
}
                }
            }
            
# now diff days and hours
            
foreach(split(",""days,hours,minutes") as $type) {
                if (
$diff >= $sec[$type]) {
                    
$result[$type] = floor($diff/$sec[$type]);
                    
$diff-= $result[$type]*$sec[$type];
                }
            }
            return 
$result $result false;
        }
        elseif(
$sec[$display]) {
            return (int)((
$target-$source) /$sec[$display]);
        }
    }

?>