Get week number (in the year) from a date PHP

前端 未结 17 2020
星月不相逢
星月不相逢 2020-11-27 14:54

I want to take a date and work out its week number.

So far, I have the following. It is returning 24 when it should be 42.



        
17条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 15:28

    Your code will work but you need to flip the 4th and the 5th argument.

    I would do it this way

    $date_string = "2012-10-18";
    $date_int = strtotime($date_string);
    $date_date = date($date_int);
    $week_number = date('W', $date_date);
    echo "Weeknumber: {$week_number}.";
    

    Also, your variable names will be confusing to you after a week of not looking at that code, you should consider reading http://net.tutsplus.com/tutorials/php/why-youre-a-bad-php-programmer/

提交回复
热议问题