How to set the “first day of the week” to Thursday in PHP

后端 未结 2 1172

I want to set the first day of the week to Thursday (not Sunday or Monday), because it\'s the company\'s cut-off date.

I already have a code to determine the current wee

2条回答
  •  逝去的感伤
    2021-02-20 04:08

    http://php.net/manual/en/datetime.formats.relative.php says that as of PHP version 5.6.23, 7.0.8 "Weeks always start on monday. Formerly, sunday would also be considered to start a week." That said, is your problem that the number of weeks returned might be incorrect depending on whether today falls on or before Thursday of the current week? Maybe try something like this:

    $date = new DateTime();
    $week = intval($date->format('W'));
    $day = intval($date->format('N'));
    echo $day < 4 ? $week-1 : $week;
    

    If subtracting 1 isn't the answer you could play around with addition/subtraction, comparing the result with the actual answer you know to be true until you get the right formula. Hope this helps!

提交回复
热议问题