How to Calculate/find Number of Days in a Month in PHP?

Problem:

Sometimes you may forget the number of days in a specific month. So, you’re looking for a solution which will show you that day number for any month.

Solution:

PHP has a built-in function named cal_days_in_month() which tells the number of days of a month for a given year and calendar. Now, we’ll use Gergorian calendar. It is one of the most commonly used calendars these days.

The following example will show you how to calculate days-

<?php
$dayNumber = cal_days_in_month(CAL_GREGORIAN, 2, 2012);
echo "There was $dayNumber days in February 2012";

echo "<br />";

$dayNumber = cal_days_in_month(CAL_GREGORIAN, 7, 2014);
echo "There was $dayNumber days in July 2014";
?>

[wpdm_file id=140]

Output:
There was 29 days in February 2012
There was 31 days in July 2014

How it Works:
The cal_days_in_month() function takes three parameters. The first one is the calendar name, the second one is the month, and the third one is the year. And, the function returns the number of days in the month of the year specified in the parameters according to the calendar.