In this guide, we will explain the βcal_days_in_month()β function, its syntax, and parameters in PHP.
What is the βcal_days_in_month()β Function in PHP?
The βcal_days_in_month()β function in the PHP programming language is very useful for finding out how many days there are in a particular month of a specific year. It is the component of PHP’s calendar extension that offers a method for handling date-related statistics and manipulations.
Syntax
The syntax of the above-discussed function is:
Parameters
There are three parameters that the “cal_days_in_month()” function accepts, and they are as follows:
β$calendarβ refers to the name of the calendar that will be used. The following constants are replaced with it according to the user’s requirements:
- CAL_GREGORIAN: Default Gregorian calendar.
- CAL_JULIAN: Julian-calendar
- CAL_JEWISH: Jewish-calendar
- CAL_FRENCH: It is the French Revolutionary calendar
β$monthβ indicates the month for which users are trying to calculate the days. The value has to range from 1 to 12.
The year where the month falls is specified by the β$yearβ parameter.
Return Value
In accordance with the provided month, year, and calendar, it will return the total number of days. It will give a false value if the calendar is not valid.
Example 1: Find the Number of Days of a Given Month and Year Using the βcal_days_in_month()β Function in PHP
In the following code, first, we initialized two integer type variables β$monthβ and β$yearβ with values of β4β and β2023β respectively. Then, invoked the βcal_days_in_month()β function that takes βCAL_GREGORIANβ, β$month1β, and β$yearβ as parameters. This built-in PHP function finds the total days in the given month and year using the Gregorian calendar. To store the result, the β$total_daysβ variable is defined. After this, use the βechoβ statement to display the output as a string. It combines the fixed phrase “Total days in April 2023 is: ” with the value of the β$total_daysβ variable:
$month1 = 4;
$year = 2023;
$total_days = cal_days_in_month(CAL_GREGORIAN, $month1, $year);
echo "Total days in April 2023 is: ". $total_days;
?>
The resultant output of the above-executed code has been shown in the following image:
Example 2: Verify the Leap Year Using the βcal_days_in_month()β Function in PHP
First, initialize the β$my_monthβ variable with the value β2β which refers to the month of February. Next, initialize the β$yearβ variable with the value of β2023β, which shows the year in which we want to determine how many days are in February. Next, invoked the βcal_days_in_month()β function that accepts three arguments, such as β$my_monthβ, β$yearβ, and βCAL_GREGORIANβ based on the Gregorian calendar. Then, the variable β$all_daysβ is used to save the result. Finally, we used the βechoβ statement for displaying the resultant value:
$my_month = 2; // February
$year = 2023; // Leap year
$all_days = cal_days_in_month(CAL_GREGORIAN, $my_month, $year);
echo "Leap Year of 2023 is: ". $all_days;
?>
Output
We have briefly described the βcal_days_in_month()β function in PHP.
Conclusion
In PHP, the βcal_days_in_month()β function is used to calculate the total days in a certain month of a given year. It is a useful resource for any task, such as requiring the creation of dynamic calendars, the verification of dates, or computations that depend on the total days present in a month. In this guide, we have stated the usage of the βcal_days_in_month()β function in PHP.