php get number days in month code example

Example 1: php days in month

// For given month
echo cal_days_in_month(CAL_GREGORIAN, 1, 2021);
// For current month
echo date('t');

Example 2: get day of month php

// get day of month php
// Method 1; some server not work, I had check php 7.3.24 not worked, php 7.3.8 worked
cal_days_in_month(CAL_GREGORIAN, $month, $year)
echo (cal_days_in_month(CAL_GREGORIAN, 2, 2020)); // => 29

// Method 2;
function days_in_month($month, $year) {
    // calculate number of days in a month
    return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}

// Method 3;
echo (date('t', strtotime('2020-02-1')));	// 29

Example 3: get month days in php

echo cal_days_in_month(CAL_GREGORIAN, 8, 2009);

Tags:

Php Example