Reputation: 882
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 ?
Upvotes: 1
Views: 67
Reputation: 1938
Try this and you should get desired result.
$prev_month_ts = strtotime('2016-06 -3 month');
$prev_month = date('Y-m', $prev_month_ts);
echo $prev_month;
Upvotes: 0
Reputation: 4166
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
Upvotes: 1
Reputation: 1374
strtotime of (Input Date ."-3 Months")
$month = 06;
$year = 2016;
$d = mktime(0, 0, 0, $month, 1, $year);
$td = date("Y-m-d", $d);
$oldDate = date('Y-m-d', strtotime($td."-3 Months"));
$oldDate_MonthYear = date('m-Y', strtotime($td."-3 Months"));
echo $oldDate;
echo '<br />';
echo $oldDate_MonthYear;
Upvotes: 1
Reputation: 19
Use Object Based.
$date1 = new DateTime("2016-06-01");
echo $startDate = $date1->modify("-3 month")->format('Y-m-d');
Upvotes: 0