To get the days in a month, you can use the php cal_days_in_month() function.

echo cal_days_in_month(CAL_GREGORIAN,4,2022);

// Output: 
30

You can also pass ‘t’ to date() to get the number of days in the current month.

echo date('t');

// Output: 
30

When working with dates in php, the ability to easily be able to get certain pieces of information from calendars and dates is very valuable.

One such piece of information is the number of days in a month.

There are a few ways you can get the number of days in a month in php. The easiest is with the php cal_days_in_month() function.

To get the number of days in a month with cal_days_in_month(), you pass the calendar you want to use, and the month and year for which you want to find the number of days.

Below is a simple example of how to use cal_days_in_month() in php to get the number of days in a month.

echo cal_days_in_month(CAL_GREGORIAN,4,2022);

// Output: 
30

How to Get the Number of Days in a Month Using date() in php

If you want to get the number of days in the current month, we can use the date() function and pass ‘t’. ‘t’ returns the last day in a month.

Below is an example in php of how to get the number of days in the current month using date().

echo date('t');

// Output: 
30

If you want to get the number of days in any month, then you can pass the timestamp of a date as the second argument to date.

echo date('t', strtotime("2022-04-01"));

// Output: 
30

Hopefully this article has helped you understand how to get the days in a month using php.

Categorized in:

PHP,

Last Update: March 13, 2024