note 100720 deleted from function.date-diff by danbrown

From: Date: Tue, 02 Nov 2010 15:01:37 +0000
Subject: note 100720 deleted from function.date-diff by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-173702@lists.php.net to get a copy of this message
Note Submitter: Diego Bretti ---- <?php function date_diff($d1, $d2) { $d1 = (is_string($d1) ? strtotime($d1) : $d1); $d2 = (is_string($d2) ? strtotime($d2) : $d2); $diff_secs = abs($d1 - $d2); $base_year = min(date("Y", $d1), date("Y", $d2)); $diff = mktime(0, 0, $diff_secs, 1, 1, $base_year); return array("years" => date("Y", $diff) - $base_year, "months" => date("n", $diff) - 1, "days" => date("j", $diff) - 1); } $now = date("Y-n-j"); $time = date_diff("2007-10-7", "$now"); $y = $time['years']; $m = $time['months']; $d = $time['days']; if($y== 0) { echo ""; } elseif($y== 1) { echo "<b>$y</b> year"; } else { echo "<b>$y</b> years"; } if($m== 0) { echo ""; } elseif($m== 1) { echo ", <b>$m</b> month"; } else { echo ", <b>$m</b> months"; } if($d== 0) { echo " together!"; } elseif($d== -1) { echo " together in <b>1</b> day!"; } elseif($d== -2) { echo " together in <b>2</b> days!"; } elseif($d== 1) { echo " and <b>$d</b> day together!"; } else { echo " and <b>$d</b> days together!"; } //Example: 2 years, 1 month and 4 days together! //Copyright © 2006 Diego Bretti ?>

« previous php.notes (#173702) next »