php

How to Use “cal_days_in_month()” Function in PHP

In PHP, developers have access to a variety of strong predefined techniques that provide them the ability to manipulate and deal with date and time. For that particular purpose, the “cal_days_in_month()” function is one of them that lets developers figure out how many days are in a given month of a particular year.

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:

cal_days_in_month(int $calendar, int $month, int $year)

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:

<?php

$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:

<?php

$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.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.