Forum Moderators: coopster
Ideally I want to add 3 months onto a given date but checking things out with 1 month gives me this headache.
Try this piece of code and you may see what I mean.
Where on earth did April go to!?
<?
$initialdate = mktime(0,0,0,03,31,2004);
print date("d-M-Y",$initialdate)."<br>";
for ($n=1;$n<=5;$n++)
{
$datemonths =date("m",$initialdate);
$datedays =date("d",$initialdate);
$dateyears =date("Y",$initialdate);
$datemonths =date("m",$initialdate);
print "DATEMONTHS ".$datemonths."<BR>";
print "DATEMONTHS PLUS ONE ".($datemonths+1)."<BR>";
$initialdate = mktime (0,0,0,($datemonths+1),$datedays,$dateyears);
print date("d-M-Y",$initialdate)."<BR>";
}
?>
31-Mar-2004
DATEMONTHS 03
DATEMONTHS PLUS ONE 4
01-May-2004
DATEMONTHS 05
DATEMONTHS PLUS ONE 6
01-Jun-2004
DATEMONTHS 06
DATEMONTHS PLUS ONE 7
01-Jul-2004
DATEMONTHS 07
DATEMONTHS PLUS ONE 8
01-Aug-2004
DATEMONTHS 08
DATEMONTHS PLUS ONE 9
01-Sep-2004
There are only 30 days in April. You have to take your day of the month into account during your loop here. See what April 31, 2004 gets you?
$initialdate = mktime (0,0,0,4,31,2004);
print date("d-M-Y",$initialdate)."<BR>";
// prints:
// 01-May-2004
Oops, almost forgot! I often use the strtotime [php.net] function for date calculations:
$month = date("F");
$next = date("F", strtotime("+1 month"));