Hi all
Will any one tell me how to get the current month and previous three months using PHP
For example:
echo date("y:M:d");
output will be: 09:Oct:20
But i need:
August
September
October
as the Output.
Thanks in advance...
Fero
Hi all
Will any one tell me how to get the current month and previous three months using PHP
For example:
echo date("y:M:d");
output will be: 09:Oct:20
But i need:
August
September
October
as the Output.
Thanks in advance...
Fero
You'll need to use date("F"); to get the full text representation of the date.
for full textual representation of month you need to pass "F":
echo date("y:F:d");
for previous month you can use
echo date("y:F:d",strtotime("-1 Months"))
;
this month
date("y:M:d", mktime(0, 0, 0, date('m'), date('d'), date('Y')));
previous months
date("y:M:d", mktime(0, 0, 0, date('m') - 1, date('d'), date('Y')));
date("y:M:d", mktime(0, 0, 0, date('m') - 2, date('d'), date('Y')));
Try using the built in strtotime
function in PHP and using 'F' for full textual output:
echo date('y:F:d'); // first month
echo date('y:F:d', strtotime('-1 month')); // previous month
echo date('y:F:d', strtotime('-2 month')); // second previous month
echo date('y:F:d', strtotime('-3 month')); // third previous month
echo date('F', strtotime('-2 month')), '<br>',
date('F', strtotime('last month')), '<br>',
date('F');
If you want to be OOP about it, try this:
$dp=new DatePeriod(date_create(),DateInterval::createFromDateString('last month'),2);
foreach($dp as $dt) echo $dt->format("y:M:d"),"\n"; //or "y F d"
outputs: