Get week number in month from date in PHP?

前端 未结 14 2190
说谎
说谎 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:02

    You can also use this simple formula for finding week of the month

    $currentWeek = ceil((date("d",strtotime($today_date)) - date("w",strtotime($today_date)) - 1) / 7) + 1;
    

    ALGORITHM :

    Date = '2018-08-08' => Y-m-d

    1. Find out day of the month eg. 08
    2. Find out Numeric representation of the day of the week minus 1 (number of days in week) eg. (3-1)
    3. Take difference and store in result
    4. Subtract 1 from result
    5. Divide it by 7 to result and ceil the value of result
    6. Add 1 to result eg. ceil(( 08 - 3 ) - 1 ) / 7) + 1 = 2

提交回复
热议问题