如何设置“一周中的第一天"到PHP的星期四 [英] How to set the "first day of the week" to Thursday in PHP

查看:37
本文介绍了如何设置“一周中的第一天"到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/zh-CN/datetime.formats.relative.php 说,从PHP版本5.6.23开始,7.0.8"周总是从星期一开始.以前,星期日也应该是考虑要开始一个星期.也就是说,您的问题是返回的星期数可能不正确,具体取决于今天是星期几还是当前星期四?也许尝试这样的事情:

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;

如果减1不是答案,则可以加/减,然后将结果与您知道是正确的实际答案进行比较,直到获得正确的公式为止.希望这会有所帮助!

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!

这篇关于如何设置“一周中的第一天"到PHP的星期四的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆