Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i get previous month name in php [duplicate]

Tags:

string

date

php

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!

like image 245
Yuda Prawira Avatar asked Dec 18 '12 13:12

Yuda Prawira


People also ask

How do I find my previous month name?

Getting the previous month name To get the previous month name, first we need to access the current date object using the new Date() constructor. const current = new Date(); Now, we need to subtract the current month with -1 and set it to the current date using the setMonth() and getMonth() methods.

How can I get last date of previous month in php?

Get Previous Month from Given Date php $date = "2021-12-01"; $new_date = date('Y-m-d', strtotime($date. ' -1 months')); echo $new_date; ?>

How can I get first day of current month in php?

//get first day of the current month $start = date("Y-m-1 00:00:00"); //get current date of the month $end = date("Y-m-d H:i:s"); //query data for the current month so far $query = $this->db_model->run_query("select column_1, column_2 from table where date_column BETWEEN '".


3 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
like image 55
MrCode Avatar answered Oct 16 '22 10:10

MrCode


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.

like image 22
Sverri M. Olsen Avatar answered Oct 16 '22 09:10

Sverri M. Olsen


use this code

$submonth = date("F", strtotime ( '-1 month' , strtotime ( 'February' ) )) ;
echo $submonth;
like image 29
Habid Pk Avatar answered Oct 16 '22 11:10

Habid Pk