Get week number in month from date in PHP?

前端 未结 14 2171
说谎
说谎 2020-11-28 11:15

I have an array of random dates (not coming from MySQL). I need to group them by the week as Week1, Week2, and so on upto Week5.

What I have is this:



        
14条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 12:01

    function getWeekOfMonth(DateTime $date) {
        $firstDayOfMonth = new DateTime($date->format('Y-m-1'));
    
        return ceil(($firstDayOfMonth->format('N') + $date->format('j') - 1) / 7);
    }
    

    Goendg solution does not work for 2016-10-31.

提交回复
热议问题