In this tutorial we'll cover how to get the first and the last day of the current month. This is particularly helpful when you want to query a database to find dates that fall within a specific month. These could be birthdays, anniversaries or just general reminders.
For the sake of this tutorial, please note that the date at the time of writing is 2023-01-16.
Get the first day of the current month
$first_day_of_month = date('Y-m-01');
echo $first_day_of_month;
The above code will output:
2023-01-01
Get the first day of any month
If you would like to get the first date of any month, use the strtotime function and pass the date as a string as shown below:
$first_day_of_month = date('Y-m-01', strtotime('next month'));
echo $first_day_of_month;
The above code will output:
2023-02-01
Get the last day of the month
$last_day_of_month = date('Y-m-t');
echo $last_day_of_month;
The above code will output:
2023-01-31
I hope you found this helpful. Happy coding.
Comments