Get week number in month from date in PHP?

前端 未结 14 2220
说谎
说谎 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 11:45

    function weekOfMonth($strDate) {
      $dateArray = explode("-", $strDate);
      $date = new DateTime();
      $date->setDate($dateArray[0], $dateArray[1], $dateArray[2]);
      return floor((date_format($date, 'j') - 1) / 7) + 1;  
    }
    

    weekOfMonth ('2015-09-17') // returns 3

提交回复
热议问题