How to usecal_days_in_month() Function in PHP

The cal_days_in_month() function is a basic method to get the number of days in a specific month.

PHP
<?php
  
$year = date("2024");
$month = date("4");
$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);

echo "Number of days in the current month: $days";
?>

Output

Number of days in the current month: 30

Explanation:

  • date(“Y”) and date(“n”): These functions return the current year and month, respectively.
  • cal_days_in_month(): This function takes three arguments: the calendar type (Gregorian in this case), the month, and the year. It returns the number of days in the specified month.

How to Get Number of Days in Current Month in PHP?

This article will show you how to get the number of days in the current month in PHP. In PHP, you may need to find the number of days in the current month for various purposes, such as generating a calendar or performing date calculations. There are three approaches to get the number of days in the current month, these are described below:

Table of Content

  • Using cal_days_in_month() Function
  • Using date() and strtotime() Functions
  • Using DateTime Objects

Similar Reads

Approach 1: Using cal_days_in_month() Function

The cal_days_in_month() function is a basic method to get the number of days in a specific month....

Approach 2: Using date() and strtotime() Functions

Another approach is to use the date() function in combination with strtotime() to calculate the number of days in the current month....

Approach 3: Using DateTime Objects

You can also use PHP’s DateTime class to get the number of days in the current month....

Contact Us