get day number of month code example

Example: get total day of month

// 3 method
// one:
function get_total_day_of_month($month, $year) {
    $day = 0;
    switch ($month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            $day     = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            $day     = 30;
            break;
        case 2:
            if ($year % 100 != 0 && $year % 4 == 0) {
                $day     = 29;
            } else {
                $day     = 28;
            }
            break;
        default: $day     = 0;
    } 
    return $day;
}

// two:
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);
}

// three: use cal_days_in_month (PHP 5.6, 7, 8)
$total_start_day_month = cal_days_in_month(CAL_GREGORIAN, 2, 2020);
echo (	$total_start_day_month );
echo ( "<br>");


// how to apply
$total_start_day_month = get_total_day_of_month(2, 2020);
echo (	$total_start_day_month );
echo ( "<br>");

$total_start_day_month = days_in_month(2, 2020);
echo (	$total_start_day_month );
echo ( "<br>");

echo (date('t', strtotime('2020-02-1')));

Tags:

Php Example