dongyun6003 2012-05-31 17:28
浏览 13

发生什么事情的日期函数[重复]

Possible Duplicate:
PHP date() and strtotime() return wrong months on 31st

I have this code and it outputs something strange i think. So, what i am doing wrong here.

<?php 
$sP1 = date('m Y');
$sP2 = date('m Y', strtotime('+01 month'));
$sP3 = date('m Y', strtotime('+02 month'));
$sP4 = date('m Y', strtotime('+03 month'));
echo $sP1.'<br>';
echo $sP2.'<br>';
echo $sP3.'<br>';
echo $sP4.'<br>';
?>

and this outputs

05 2012
07 2012
07 2012
08 2012

i think the second one should be

06 2012

Anybody know any solution?

  • 写回答

3条回答 默认 最新

  • dpgkg42484 2012-05-31 17:30
    关注

    Today is the 31st next month only has 30 days so it would be 7/12 in 1 month from today

    assuming that today is May 31 2012
    
    date('m Y') == 05 2012
    date('m Y', strtotime('+1 month')) == 07 2012 because june has 30 days
    date('m Y', strtotime('+2 month')) == 07 2012
    date('m Y', strtotime('+3 month')) == 08 2012
    date('m Y', strtotime('+4 month')) == 10 2012
    

    I would take today's date and find the first day of the month then add a month to that if you are doing something that needs to get each month

    评论

报告相同问题?