note 111739 added to datetime.diff
| From: | theanil at yahoo dot com | Date: | Fri, 22 Mar 2013 11:08:20 +0000 |
| Subject: | note 111739 added to datetime.diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194174@lists.php.net to get a copy of this message | ||
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);
----
Server IP: 69.147.83.199
Probable Submitter: 14.140.53.178
----
Manual Page -- http://php.net/manual/en/datetime.diff.php
Edit -- https://master.php.net/note/edit/111739
Del: integrated -- https://master.php.net/note/delete/111739/integrated
Del: useless -- https://master.php.net/note/delete/111739/useless
Del: bad code -- https://master.php.net/note/delete/111739/bad+code
Del: spam -- https://master.php.net/note/delete/111739/spam
Del: non-english -- https://master.php.net/note/delete/111739/non-english
Del: in docs -- https://master.php.net/note/delete/111739/in+docs
Del: other reasons-- https://master.php.net/note/delete/111739
Reject -- https://master.php.net/note/reject/111739
Search -- https://master.php.net/manage/user-notes.php