Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get previous month value in PHP

Tags:

php

I have used the below PHP function to get the previous month,

$currmonth = date('m', strtotime('-1 month'));

It was working fine and I was getting the value of 04 till yesterday. On today May 31st (Last day of the month May), I noticed the function returns the current month only. That is 05. Is there any other alternate function which returns the previous month accurately.

like image 927
Nuju Avatar asked May 31 '17 11:05

Nuju


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 to get Current month and last month in Php?

php echo date("M - Y"). "\n"; echo date("M - Y",strtotime("-1 Months")). "\n"; echo date("M - Y",strtotime("-2 Months")). "\n"; echo date("M - Y",strtotime("-3 Months")).


1 Answers

Try strtotime("first day of last month").

The first day of is the important part as detailed here.

like image 187
bamtheboozle Avatar answered Oct 18 '22 20:10

bamtheboozle