Get all days and date for a given month

后端 未结 9 1511
天涯浪人
天涯浪人 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:51

    I'm surprised nobody mentioned PHP's built-in, no extra extensions DateTime class.

    format('t');
    print_r($daysInAugust2020);
    

    You'll have to append some day to your YYYYMM string, but it's not that hard: every month has a 1st. So you can always add 01.

    The trick here is the t format, which just returns the number of days in the month the date is in. If you're trying to get this month's amount of days, it's even easier. Just running the date() function creates a DateTime set to right now, so the above code becomes:

提交回复
热议问题