How to find the last day of the month from date?

后端 未结 28 1953
孤城傲影
孤城傲影 2020-11-22 08:56

How can I get the last day of the month in PHP?

Given:

$a_date = \"2009-11-23\"

I want 2009-11-30; and given

$a_dat         


        
28条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 09:20

    I am late but there are a handful of easy ways to do this as mentioned:

    $days = date("t");
    $days = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));
    $days = date("j",mktime (date("H"),date("i"),date("s"),(date("n")+1),0,date("Y")));
    

    Using mktime() is my go to for complete control over all aspects of time... I.E.

    echo "
    ".date("Y-n-j",mktime (date("H"),date("i"),date("s"),(11+1),0,2009));

    Setting the day to 0 and moving your month up 1 will give you the last day of the previous month. 0 and negative numbers have the similar affect in the different arguements. PHP: mktime - Manual

    As a few have said strtotime isn't the most solid way to go and little if none are as easily versatile.

提交回复
热议问题