note 61259 deleted from function.mktime by derick

From: Date: Sun, 24 Sep 2006 11:50:00 +0000
Subject: note 61259 deleted from function.mktime by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-117497@lists.php.net to get a copy of this message
Note Submitter: Fabrizio (staff at bibivu dot com) ---- here a function that will help to find the difference between two dates, and it will take into consideration the leaps years this function will return an array with: YEARS (years) MONTHS (months) DAYS (days) HOURS (hours) MINUTES (minutes) SECONDS (seconds) probably can be optimized, but for now does the work -------------------------------------------------------- Una funzione che aiuta a trovare la differenze tra due date, prendendo in considerazione anche gli anni bisestili. Questa funzione ritorna un array con: ANNI (years) MESI (months) GIORNI (days) ORE (hours) MINUTI (minutes) SECONDI (seconds) probabilmente puo' essere ottimizzata, ma per ora funziona ---------------------------------------------------------- <?php function bib_datediff($fromtime, $totime=''){ if($totime=='') $totime = time(); if($fromtime>$totime){ $tmp = $totime; $totime = $fromtime; $fromtime = $tmp; } $timediff = $totime-$fromtime; //check for leap years in the middle for($i=date('Y',$fromtime); $i<=date('Y',$totime); $i++){ if ((($i%4 == 0) && ($i%100 != 0)) || ($i%400 == 0)) { $timediff -= 24*60*60; } } $remain = $timediff; $ret['years'] = intval($remain/(365*24*60*60)); $remain = $remain%(365*24*60*60); $ret['days'] = intval($remain/(24*60*60)); $remain = $remain%(24*60*60); $m[0] = 31; $m[1] = 28; $m[2] = 31; $m[3] = 30; $m[4] = 31; $m[5] = 30; $m[6] = 31; $m[7] = 31; $m[8] = 30; $m[9] = 31; $m[10] = 30; $m[11] = 31; //if leap year, february has 29 days if (((date('Y',$totime)%4 == 0) && (date('Y',$totime)%100 != 0)) || (date('Y',$totime)%400 == 0)){ $m[1] = 29; } $ret['months'] = 0; foreach($m as $value){ if($ret['days']>$value){ $ret['months']++; $ret['days'] -=$value; } else { break; } } $ret['hours'] = intval($remain/(60*60)); $remain = $remain%(60*60); $ret['minutes'] = intval($remain/60); $ret['seconds'] = $remain%60; return $ret; } ?>

« previous php.notes (#117497) next »