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

后端 未结 2 1137

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!

    0 讨论(0)
  • 2021-02-20 04:25

    This should work.

    function findweek($date, $type = "l") {
        $time = strtotime($date);
        return date($type, mktime(0, 0, 0, date("m", $time) , date("d", $time)-date("d", $time)+1, date("Y", $time)));
    }
    
    echo findweek('2015-09-16');
    
    0 讨论(0)
提交回复
热议问题