Home » Php » string – how can i get previous month name in php

string – how can i get previous month name in php

Posted by: admin July 12, 2020 Leave a comment


Possible Duplicate:
next and previous month from a given month in php

I use this code :

$prev_month = strtolower(date('F',strtotime('February - 1 month')));

and always return December even i change the month into March.

Please help!

How to&Answers:
$currentMonth = date('F');
echo Date('F', strtotime($currentMonth . " last month"));

If you don’t want it relative to the current month then set:

$currentMonth = 'February';
// outputs January


use this code

$submonth = date("F", strtotime ( '-1 month' , strtotime ( 'February' ) )) ;
echo $submonth;


strtotime() understand 'last month'.

$last_month = date('F', strtotime('last month'));

You can also use the \DateTime class:

$date_time = new \DateTime('last month');
$last_month = $date_time->format('F');

It depends on what you need. If you only want the name of the previous month then the first example is fine. If you want to play with the dates (such as loop over the months in the year) then the \DateTime class makes that really easy.


Use the following code:

echo date("F", time()-date("j")*24*60*60);

You can also use:

echo date("F", strtotime("last month"));



 $days_passed_this_month = date("j");

    $timestamp_last_month = time() - $days_passed_this_month *24*60*60;

    $last_month = date("F", $timestamp_last_month);

just play around with the date() and time() functions and see where you get.