PHP date('W') vs MySQL YEARWEEK(now())

后端 未结 3 2058
情深已故
情深已故 2021-01-01 23:04

Can someone kindly explain me why these two give different results?

I execute this with PHP.

date(\"YW\",mktime(0, 0, 0, 3, 22 , 2013)); // outputs 2         


        
3条回答
  •  星月不相逢
    2021-01-01 23:39

    YEARWEEK takes a second (optional) parameter that specifies the range of the week [0- 53] or [1-53]).

    This function returns the week number for date. The two-argument form of WEEK() enables you to specify whether the week starts on Sunday or Monday and whether the return value should be in the range from 0 to 53 or from 1 to 53. If the mode argument is omitted, the value of the default_week_format system variable is used.

    while date(W) is an ISO8601 date that is always in the range [01-53]. Therefore my guess is that by default YEARWEEK is using the [0-53] range.

    So, if you want to get the same result try using 1 as the second parameter for YEARWEEK

提交回复
热议问题