note 93972 modified in function.strtotime by danbrown
| From: | danbrown@php.net | Date: | Fri, 09 Oct 2009 16:18:23 +0000 |
| Subject: | note 93972 modified in function.strtotime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-161592@lists.php.net to get a copy of this message | ||
lot of people said that there is bug in strtotime method like given in this example:
<?php echo date( "Y-m-d", strtotime( "2009-01-31 +1 months" )
)."<br>"; ?>
this gives 03-03-2009 instead of 28-02-2009
here is work arround for this bug.
<?php
/// n=no of months , $y =year, $m = month, $d = day , $t = + or -
function NewMonthDate($n,$y,$m,$d,$t)
{
for($i=1; $i <= $n; $i++)
{
if($t=="+")
{
$pmonth=$m+$i;
}
if($t=="-")
{
$pmonth=$m-$i;
}
$pyear=$y;
$aa=1;
/////////////////this will show lastmonth//////////////
$lastmonth = mktime(0, 0, 0, $pmonth, $aa, $pyear);
$smonth=date("m",$lastmonth);
$syear=date("Y",$lastmonth);
$ndays=date("t",$lastmonth);
$valarray=array($smonth,$syear,$ndays);
$mm=$mm+$ndays;
}
$basedate="$pyear-$m-$d";
$date2 = strtotime("$basedate $t$mm days");
$date1=date("d-m-Y",$date2);
return $date1;
} ////
$n=1;
$y=2009;
$m=01;
$d=31;
$t="+";
$rr=NewMonthDate($n,$y,$m,$d,$t);
print_r($rr);
?>
--was--
lot of people said that there is bug in strtotime method like given in this example:
echo date( "Y-m-d", strtotime( "2009-01-31 +1 months" )
)."<br>";
this gives 03-03-2009 instead of 28-02-2009
here is work arround for this bug.
/// n=no of months , $y =year, $m = month, $d = day , $t = + or -
function NewMonthDate($n,$y,$m,$d,$t)
{
for($i=1; $i <= $n; $i++)
{
if($t=="+")
{
$pmonth=$m+$i;
}
if($t=="-")
{
$pmonth=$m-$i;
}
$pyear=$y;
$aa=1;
/////////////////this will show lastmonth//////////////
$lastmonth = mktime(0, 0, 0, $pmonth, $aa, $pyear);
$smonth=date("m",$lastmonth);
$syear=date("Y",$lastmonth);
$ndays=date("t",$lastmonth);
$valarray=array($smonth,$syear,$ndays);
$mm=$mm+$ndays;
}
$basedate="$pyear-$m-$d";
$date2 = strtotime("$basedate $t$mm days");
$date1=date("d-m-Y",$date2);
return $date1;
} ////
$n=1;
$y=2009;
$m=01;
$d=31;
$t="+";
$rr=NewMonthDate($n,$y,$m,$d,$t);
print_r($rr);
http://php.net/manual/en/function.strtotime.php