How to Get First Day of Month from Date in PHP

How to Get First Day of Month from Date in PHP?

  • Post category:PHP
  • Post comments:2 Comments

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. You Can Learn How to Get Time Difference in Hours and Minutes in PHP Tutorials?

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:

How to Get First Day of Month from Date in PHP 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.

This Post Has 2 Comments

Leave a Reply