2017-04-13 108 views
-2

星期六星期六(星期六 - >星期五)以星期六为基准的星期数?我一直在解决这个问题时遇到了很多麻烦。 PHP解决方案将不胜感激,但任何语言的任何示例都可以。星期六开始星期的星期数

我的公司工作周从星期六(第1天)到星期五(第7天),我需要能够显示周数(如52/53周第23周)。我可能会感到困惑,并且正常的Date('W', $time)函数给出的周数是足够的,但我对此表示怀疑。

我在Excel中找到了可能的解决方案,但是我很长一段时间没有做过VBA,也无法制作正面和反面的this implementation

+0

周依据是什么号码?自今年年初以来? http://stackoverflow.com/q/4896452/56778对你有什么用处?本页面显示的其他相关问题如何(下方和右方)?或者,也许http://stackoverflow.com/q/30056515/56778将是有用的。你需要做一点研究。 –

+0

你如何定义“周编号”?是从某个日期开始还是你是指一周内的数字(星期六= 1,星期日= 2等)? – toonice

+0

请正确地扩展您的需求。即具有样本投入和预期产出,特别是在一年左右的日期。如果你有一个基于本周不同开始日期的工作实施,我认为应该可以通过抵消这一天来伪造它。 –

回答

0

这些功能似乎解决我的问题,但更遥远的多年试验可能是必要的:

// 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); 
} 
相关问题