I want to get last 3 months name from current month. For example current month is August. So, I want the datas like these June, July, August. I have tried this code echo date("F", strtotime("-3 months"));
. it returns June only. How do I get last 3 months from current month using php?
Asked
Active
Viewed 4,624 times
0

Karuppiah RK
- 3,894
- 9
- 40
- 80
-
2Could you just create a `for` loop and decrement the subtraction each time adding the result to an array? – carloabelli Aug 06 '14 at 05:07
4 Answers
4
Simple code:
<?php
for($x=2; $x>=0;$x--){
echo date('F', strtotime(date('Y-m')." -" . $x . " month"));
echo "<br>";
}
?>
Got the idea from LTroubs here.

Community
- 1
- 1

kimbarcelona
- 1,136
- 2
- 8
- 19
4
You are actually on the right track by using date and strtotime functions. Expanding it below to your requirements:
function getMonthStr($offset)
{
return date("F", strtotime("$offset months"));
}
$months = array_map('getMonthStr', range(-3,-1));

coder.in.me
- 1,048
- 9
- 19
3
That should work:
function lastThreeMonths() {
return array(
date('F', time()),
date('F', strtotime('-1 month')),
date('F', strtotime('-2 month'))
);
}
var_dump(lastThreeMonths());

laurent
- 88,262
- 77
- 290
- 428
0
Try this:-
$current_date = date('F');
for($i=1;$i<3;$i++)
{
${"prev_mont" . $i} = date('F',strtotime("-$i Months"));
echo ${"prev_mont" . $i}."</br>";
}
echo $current_date."</br>";

Kavita
- 77
- 7