note 111739 deleted from datetime.diff by salathe

From: Date: Fri, 22 Mar 2013 14:52:01 +0000
Subject: note 111739 deleted from datetime.diff by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-194175@lists.php.net to get a copy of this message
Note Submitter: theanil at yahoo dot com ---- I have written two function this work properly in 5.2 as well 5.3 ---------------------------------- function noofdays($month,$year) { $leap = (($year % 4 ) == 0) ? "1" : "0"; $MyDate = array("01" => 31, "02" => 28, "03" => 31, "04" => 30, "05" => 31, "06" => 30, "07" => 31, "08" => 31, "09" => 30, "10" => 31, "11" => 30, "12" => 31); $nodays = $MyDate[$month]; if($leap) { if($month == '02') { $nodays++; } } return $nodays; } function osdate_diff($date1, $date2) { $oneday = 86400; $pieces1 = explode("-", $date1); $pieces2 = explode("-", $date2); $d1 = mktime(0, 0, 0, $pieces1[1], $pieces1[2], $pieces1[0]); $d2 = mktime(0, 0, 0, $pieces2[1], $pieces2[2], $pieces2[0]) + $oneday; #print "<br> d1 $d1 <br> d2 $d2 "; $datediff = ($d2 - $d1)/$oneday; $datediff = floor($datediff); #print "<br> diff " . $datediff;//exit; $months = 0; $dremind = $datediff; #echo "<br> datediff $datediff ";//exit; $m = $pieces1[1]; $y = $pieces1[0]; #echo "<br> $m $y "; #print "<br> " . noofdays($m,$y); $mm = $datediff; do { #echo "<br> dremind $dremind "; if($dremind >= noofdays($m,$y)) { #echo "<br>0 m = $m - y = $y && mm $mm y = $y"; $d2 = noofdays($m,$y); #echo "<br>1 dremind $dremind d2 $d2 (m = $m y = $y)"; $mm = $dremind - $d2; $months++; $dremind = $dremind - noofdays($m,$y); $m = intval($m) + 1; if(strlen($m) == 1) { $m = "0" . $m; } if($m > 12) { $m = "01"; $y = $y+1; } } if($dremind == 0) { break; } } while ($dremind >= noofdays($m,$y)); $yy = floor(($months/12)); if($yy >0) { $months = $months - (12 * $yy); } $diff = array(); $diff["year"] = $yy; $diff["month"] = $months; $diff["day"] = $dremind; return $diff; } $date_from = "2013-04-01"; $date_to = "2013-04-30"; #$date_to = "2014-04-31"; print "<br>\n" . $date_from; print "<br>\n" . $date_to; $diff = osdate_diff($date_from, $date_to); print "<br><br>\n\n"; print_r($diff);

« previous php.notes (#194175) next »