note 97290 deleted from function.time by danbrown

From: Date: Mon, 12 Apr 2010 20:48:47 +0000
Subject: note 97290 deleted from function.time by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168175@lists.php.net to get a copy of this message
Note Submitter: LWC ---- Here's a more advanced version of the function. But it still plainly assumes there are 31 days in each month: <?php function date_diff($sdate, $edate, $minimal_text=true, $zerotoo=false) { $display = ''; $definition = array('year' => 60*60*24*31*12, 'month' => 60*60*24*31, 'day' => 60*60*24, 'hour' => 60*60, 'minute' => 60, 'second' => 1); $time = $edate - $sdate; if ($minimal_text) $zerotoo=true; foreach ($definition as $key=>$value) { $level = -1; if ($time >= $value) { $divide = $time/$value; $level = floor($divide); $time -= $level*$value; } elseif ($time == 0 && $zerotoo) $level = 0; if ($level > -1) { $key .= 's'; if (!$minimal_text) $display .= "$level $key "; else { if ($value < $definition['day']) { if ($level < 10) $level = "0$level"; $display .= "$level:"; } else $display .= "$level " . $key[0] . " "; } } } $display = trim($display, " :"); if ($minimal_text) { $checkformat = explode(':', $display); if (count($checkformat)-1 < 2) for ($i=2;$i>0;$i--) if (count($checkformat)-1 < $i) $display = "00:$display"; } return $display; } echo date_diff(time(), time()+31) . "\n<hr>"; echo date_diff(time(), time()+61) . "\n<hr>"; echo date_diff(time(), time()+601) . "\n<hr>"; echo date_diff(time(), time()+6001) . "\n<hr>"; echo date_diff(time(), time()+60001) . "\n<hr>"; echo date_diff(time(), time()+36000, true) . "\n<hr>"; echo date_diff(time(), time()+6000001) . "\n<hr>"; echo date_diff(time(), time()+60000001) . "\n<hr><hr>"; ?>

« previous php.notes (#168175) next »