How to Get First Day of Month from Date in PHP?
Hi Folks,
In this example, I will show you php get first day of month from date. This example will help you php date first day of month from date. This article will give you a simple example of how to get first day of month from date in php. we will help you to give an example of php find first day of month from date. So, let's follow a few steps to create an example of php get first day of month from date example.
In this example, i will give you following two simple examples with output to find first day of month from date in php. so, let's see the one by one example:
Example 1:
Certainly! Here's an example of how to use date('Y-m-01') to get the first day of the current month in PHP:
index.php
<?php
$firstDayOfMonth = date('Y-m-01');
echo "The first day of the current month is: " . $firstDayOfMonth;
?>
Output:
The first day of the current month is: 2024-01-01
It uses the date() function with the format 'Y-m-01' to get the current year and month, and sets the day to 01 to get the first day of the month.
Example 2:
To get the first day of the month from a given date in PHP, you can use the "date()" function along with the "strtotime()" function. Here's an example:
index.php
<?php
$date = "2024-01-15"; /* Replace this with your date in "YYYY-MM-DD" format */
$firstDayOfMonth = date("Y-m-01", strtotime($date));
echo "The first day of the month is: " . $firstDayOfMonth;
?>
Output:
2024-01-01
In this example, the "strtotime()" function is used to convert the input date string into a Unix timestamp, and then the "date()" function is used to format that timestamp to display only the year, month, and the first day of the month. Adjust the input date variable as needed.
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Convert DateTime to Timestamp in PHP?
- How to Convert Datetime into Milliseconds in PHP?
- How to Increase Session Timeout in PHP?
- How to Extract Img SRC, Title and Alt from HTML in PHP?
- How to Convert Bytes into KB, MB and GB in PHP?
- How to Convert Array to XML in PHP?
- How to Upgrade PHP Version from 8.2 to 8.3 in Ubuntu?
- PHP Check If Array Has Duplicate Values Example
- How to Create Zip Folder and Download in PHP?
- PHP MySQL Contact US Form with Validation Example
- PHP MySQL Image Gallery CRUD Example
- PHP Ajax Dependent Dropdown List Example