这些功能似乎解决我的问题,但更遥远的多年试验可能是必要的:
// The N option gives the normal week day, arithmetic makes it return my custom week days
function getDayOfWeek(\DateTimeImmutable $date)
{
return ($date->format('N') + 2) % 7;
}
function getWeekNumber(\DatetimeImmutable $date)
{
// Recursive function that loops through every day until it gets the start of a week
$startOfWeek = function (\DateTimeImmutable $date) use (&$startOfWeek) {
return (getDaysOfWeek($date) === 1)
? $date : $startOfWeek($date->modify('+1 day'));
};
// The z option tells us what day of the year this is,
// not a 100% sure this step is necessary
if (getDayOfWeek($date === 1) {
$nbDays = $date->format('z');
else {
$nbDays = $startOfWeek($date->modify('-7 days'))->format('z');
}
// Divides by the number of days in a week to get the number of weeks
return ceil($nbDays/7);
}
周依据是什么号码?自今年年初以来? http://stackoverflow.com/q/4896452/56778对你有什么用处?本页面显示的其他相关问题如何(下方和右方)?或者,也许http://stackoverflow.com/q/30056515/56778将是有用的。你需要做一点研究。 –
你如何定义“周编号”?是从某个日期开始还是你是指一周内的数字(星期六= 1,星期日= 2等)? – toonice
请正确地扩展您的需求。即具有样本投入和预期产出,特别是在一年左右的日期。如果你有一个基于本周不同开始日期的工作实施,我认为应该可以通过抵消这一天来伪造它。 –