Home >Backend Development >PHP Tutorial >How Can I Find the Last Day of a Month Using PHP?

How Can I Find the Last Day of a Month Using PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-29 07:42:10194browse

How Can I Find the Last Day of a Month Using PHP?

Finding the Last Day of the Month in PHP

To obtain the last day of any given month in PHP, we can leverage a combination of the date and strtotime functions. Here's how:

Answer:

The date function, when passed the "t" format specifier, combined with the timestamp generated by strtotime for a given date, provides the number of days in that month.

Example:

To retrieve the last day of November 2009:

$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));

Output:

2009-11-30

Similarly, for December 2009:

$a_date = "2009-12-23";
echo date("Y-m-t", strtotime($a_date));

Output:

2009-12-31

The above is the detailed content of How Can I Find the Last Day of a Month Using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn