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!
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.
Get Previous Month from Given Date php $date = "2021-12-01"; $new_date = date('Y-m-d', strtotime($date. ' -1 months')); echo $new_date; ?>
//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 '".
$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
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 this code
$submonth = date("F", strtotime ( '-1 month' , strtotime ( 'February' ) )) ;
echo $submonth;
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