Get all days and date for a given month

后端 未结 9 1531
天涯浪人
天涯浪人 2021-02-10 08:17

Would like to retrieve all days and date for a given month. Have this currently which shows all the days for the current month, but how do I parse in a specified month instead?<

9条回答
  •  日久生厌
    2021-02-10 08:50

    Take advantage of the relative formats.

    $y_m = '2018-10'; // set year and month.
    
    $list = array();
    $d = date('d', strtotime('last day of this month', strtotime($y_m))); // get max date of current month: 28, 29, 30 or 31.
    
    for ($i = 1; $i <= $d; $i++) {
        $list[] = $y_m . '-' . str_pad($i, 2, '0', STR_PAD_LEFT);
    }
    
    echo '
    ';
    print_r($list);
    echo '
    ';

提交回复
热议问题