Nevermore Nevermore - 7 months ago 11
PHP Question

How to get third previous month from inputted month-year?

I am trying to get third previous month from inputted month-year ie. if I give

06-2016
as an input, it should give me
03-2016
as result. I have tried
strtotime("-3 Months")
. But it gives me just 3 months from current date time.

Can someone tell me how to solve it please.

I have tried:

$month = 06;
$year = 2016;

$d = mktime(0, 0, 0, $month, 1, $year);
$td = date("Y-m-d", $d);
$oldDate = date('Y-m-d', strtotime("-3 Months"));


It gives me 3 months from current date. Any guesses how do I get the expected result ?

Answer

Use below. You need to use date("F Y", strtotime( INPUT_DATE ." -3 months"))

$month = 06;
$year = 2016;

$d = mktime(0, 0, 0, $month, 1, $year);
$td = date("Y-m-d", $d);
echo $olddate = date("Y-m-d", strtotime( $td ." -3 months"));
echo $months = date("F Y", strtotime( $td ." -3 months"));

Output:

2016-03-01
March 2016

Online Demo: Click Here