如何在PHP中将“一周的第一天”设置为星期四

时间:2022-04-20 08:16:46

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 week number of a date but it starts in Sunday or Monday.

我已经有一个代码来确定一个日期的当前周数,但是它从周日或周一开始。

How to modify these to my preference?

如何根据我的喜好修改这些?

function findweek($date) {
    $monthstart=date("N",strtotime(date("n/l/Y",strtotime($date))));
    $newdate=(date("j",strtotime($date))+$monthstart)/7;
    $ddate=floor($newdate);
    if($ddate != $date) {
        $ddate++;
    }
    return $ddate;
}

2 个解决方案

#1


1  

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');

#2


1  

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:

http://php.net/manual/en/datetime.formats.relative.php表示,自PHP版本5.6.23起,7.0.8“周总是在星期一开始。以前,星期日也会被认为是开始一周“。那就是问题,根据今天是在今周的星期四或之前,返回的周数可能是不正确的?也许尝试这样的事情:

$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!

如果减去1不是您可以使用加/减的答案,将结果与您知道的实际答案进行比较,直到您得到正确的公式。希望这可以帮助!

#1


1  

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');

#2


1  

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:

http://php.net/manual/en/datetime.formats.relative.php表示,自PHP版本5.6.23起,7.0.8“周总是在星期一开始。以前,星期日也会被认为是开始一周“。那就是问题,根据今天是在今周的星期四或之前,返回的周数可能是不正确的?也许尝试这样的事情:

$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!

如果减去1不是您可以使用加/减的答案,将结果与您知道的实际答案进行比较,直到您得到正确的公式。希望这可以帮助!