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.
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.
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")).
Try strtotime("first day of last month")
.
The first day of
is the important part as detailed here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With