Hi everyone
When looping and incrementing dates, I noticed that one date was incrementing incorrectly. On checking the code I realised I'd done it a rather poor way and fixed it, but does
anyone know why I get the results shown?
$monthnumber = 1;
$from = date('Y-m-01');
$to = date('Y-m-t');
while ($monthnumber < 4) {
print ("Monthnumber $monthnumber From $from to $to<br />");
$from = strtotime($from);
$from = date("Y-m-01", strtotime("+1 month", $from));
// returns an incorrect $to date for February
// $to = strtotime($to);
// $to = date("Y-m-t", strtotime("+1 month", $to));
// this works properly (and is obviously neater)
$to = date("Y-m-t", strtotime($from));
$monthnumber++;
} // done all months
Correct output from code above:
Monthnumber 1 From 2012-12-01 to 2012-12-31
Monthnumber 2 From 2013-01-01 to 2013-01-31
Monthnumber 3 From 2013-02-01 to 2013-02-28
Incorrect output from code if I revert to the commented out $to code:
Monthnumber 1 From 2012-12-01 to 2012-12-31
Monthnumber 2 From 2013-01-01 to 2013-01-31
Monthnumber 3 From 2013-02-01 to 2013-03-31
Thank you