Home > Article > Backend Development > How to Find the First and Last Dates of a Month in PHP?
Finding the First and Last Dates in a Month Using PHP
PHP offers a powerful function called date that allows you to retrieve and format various information from timestamps. This includes finding the first and last dates in a given month.
Finding Dates for the Current Month
To find the first and last dates of the current month, you can simply specify the "m-01-Y" and "m-t-Y" format strings, respectively. These represent the first day of the month (with a hard-coded '01') and the last day of the month (indicated by 't'). For example:
<code class="php">$first_day_this_month = date('m-01-Y'); // hard-coded '01' for first day $last_day_this_month = date('m-t-Y');</code>
Finding Dates for Specific Months
To find the first and last dates for a specific month, you can specify a timestamp. This can be a string in a recognized date format or a UNIX timestamp. For example, to find the last day of April 2010:
<code class="php">$last_day_april_2010 = date('m-t-Y', strtotime('April 21, 2010'));</code>
Understanding the Format Symbols
date() interprets specific symbols within the format string and replaces them with values from the timestamp. The symbols used for extracting month and day values are:
Additional Examples
<code class="php">$timestamp = strtotime('February 2012'); $first_second = date('m-01-Y 00:00:00', $timestamp); $last_second = date('m-t-Y 12:59:59', $timestamp);</code>
For more detailed information on the date() function and available format symbols, refer to the official PHP manual: http://php.net/manual/en/function.date.php.
The above is the detailed content of How to Find the First and Last Dates of a Month in PHP?. For more information, please follow other related articles on the PHP Chinese website!