简体   繁体   English

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

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

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;
}

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? http://php.net/manual/en/datetime.formats.relative.php表示,自PHP版本5.6.23起,7.0.8“ 周总是在星期一开始。以前,星期日也会被认为是开始一周“这就是说,你的问题是返回的周数可能会因今天是否落在或本周的周四之前是不正确的? 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. 如果减去1不是您可以使用加/减的答案,将结果与您知道的实际答案进行比较,直到您得到正确的公式。 Hope this helps! 希望这可以帮助!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM