note 97291 deleted from function.date-diff by danbrown

From: Date: Mon, 12 Apr 2010 20:48:53 +0000
Subject: note 97291 deleted from function.date-diff by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168176@lists.php.net to get a copy of this message
Note Submitter: LWC ---- Here's an advanced version of the function, but it still just 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; } ?>

« previous php.notes (#168176) next »