note 102601 deleted from function.date-diff by danbrown
[email protected] Wed, 23 Feb 2011 19:05:38 -0800
| Newsgroups | php.notes |
|---|---|
| Message-ID | <[email protected]> |
Note Submitter: jrs from atlanticbt.com
----
Based in part on previous try, which didn't work for me :( -- http://us3.php.net/manual/en/function.date-diff.php#101771
function dateDiff($timestamp1, $timestamp2, $format = false){
$date1 = getdate($timestamp1);
$diff = getdate($timestamp2);
//find the differences between the two
foreach($diff as $aspect => &$value){
$value -= $date1[$aspect];
}
//now adjust for negative numbers
if( $diff['seconds'] < 0 ){
$diff['seconds'] += 60;
$diff['minutes']--;
}
//now adjust for negative numbers
if( $diff['minutes'] < 0 ){
$diff['minutes'] += 60;
$diff['hours']--;
}
//now adjust for negative numbers
if( $diff['hours'] < 0 ){
$diff['hours'] += 24;
$diff['mday']--;
}
//now adjust for negative numbers; special for days!
if( $diff['mday'] < 0 ){
$diff['mday'] += cal_days_in_month(CAL_GREGORIAN, $date1['mon'], $date1['year']);
$diff['mon']--;
}
//now adjust for negative numbers
if( $diff['mon'] < 0 ){
$diff['mon'] += 12;
$diff['year']--;
}
//now adjust for negative numbers
if( $diff['year'] < 0 ){
return 'WTF?';
}
//return formatted, or just the diff
if($format){
return str_replace(
array(
'%y'
,'%m'
,'%d'
,'%H'
,'%M'
,'%S'
)
,array(
$diff['years']
, $diff['mon']
, $diff['mday']
, $diff['hours']
, $diff['minutes']
, $diff['seconds']
)
, $format
);
}
return $diff;
}