In PHP, is there an easy way to get the first and last date of a month?

后端 未结 11 958
无人及你
无人及你 2020-12-01 11:47

I need to get the first and last day of a month in the format YYYY-MM-DD given only the month and year. Is there a good, easy way to do this?

11条回答
  •  旧时难觅i
    2020-12-01 12:30

    OK, first is dead easy.

    date ('Y-m-d', mktime(0,0,0,MM,01,YYYY));
    

    Last is a little trickier, but not much.

    date ('Y-m-d', mktime(0,0,0,MM + 1,-1,YYYY));
    

    If I remember my PHP date stuff correctly...

    **edit - Gah! Beaten to it about a million times...

    Edit by Pat:

    Last day should have been

    date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1
    

提交回复
热议问题