How to get the previous and next month?

前端 未结 8 670
旧巷少年郎
旧巷少年郎 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:14

    try it like this:

    $date = mktime(0, 0, 0, $month, 1, $year);
    echo date("Y-m", strtotime('-1 month', $date));
    echo date("Y-m", strtotime('+1 month', $date));
    

    or, shorter, like this:

    echo date("Y-m", mktime(0, 0, 0, $month-1, 1, $year));
    echo date("Y-m", mktime(0, 0, 0, $month+1, 1, $year));
    

提交回复
热议问题