How to get the previous and next month?

前端 未结 8 672
旧巷少年郎
旧巷少年郎 2021-01-02 04:01
$year  = 2010;
$month = 10;

How do I get the previous month 2010-09 and next month 2010-11?

相关标签:
8条回答
  • 2021-01-02 04:28

    strftime *:-* Format the time and/or date according to locale settings. Month and weekday names and other language-dependent strings respect the current locale set with setlocale().

    strftime( '%B %Y', strtotime( '+1 month', $date ) );
    strftime( '%B %Y', strtotime( '-1 month', $date ) );
    
    0 讨论(0)
  • 2021-01-02 04:30

    PHP is awesome in this respect, it will handle date overflows by correcting the date for you...

    $PreviousMonth = mktime(0, 0, 0, $month - 1, 1, $year);
    $CurrentMonth = mktime(0, 0, 0, $month, 1, $year);
    $NextMonth = mktime(0, 0, 0, $month + 1, 1, $year);
    
    echo '<p>Next month is ' . date('Ym', $NextMonth) . 
        ' and previous month is ' . date('Ym', $PreviousMonth . '</p>';
    
    0 讨论(0)
提交回复
热议问题