How can I get the name of the previous month in php

Possible duplicate:
next and previous month from the given month in php

I am using this code:

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

and always return December, even change month by month.

Please, help!

+6
source share
5 answers
 $currentMonth = date('F'); echo Date('F', strtotime($currentMonth . " last month")); 

If you do not want it to refer to the current month, set:

 $currentMonth = 'February'; // outputs January 
+12
source

use this code

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

Use the following code:

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

You can also use:

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

PHP code

  $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 with the date () and time () functions and see where you are.

0
source

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 want only the name of the previous month, then the first example will be wonderful. If you want to play with dates (for example, a cycle for months in a year), the \DateTime class makes this very simple.

0
source

All Articles