Alex Rivera | Logout

How to get the previous and next month?

Asked 2010-10-14T06:57:27.650
11
$year  = 2010;
$month = 10;

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

Edit
Report

1 Answer

4

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));
answered 2010-10-14T07:31:14.697

Your Answer