您如何根据星期六作为一周的开始日(星期六 - > 星期五)找到周数?我在弄清楚这个问题时遇到了过多的麻烦。PHPsolution 将不胜感激,但任何语言的任何示例也可以。
我公司的工作周是从星期六(第 1 天)到星期五(第 7 天),我需要能够显示周数(如 52/53 的第 23 周)。我可能会感到困惑,正常Date('W', $time)
功能给出的周数就足够了,但我对此表示怀疑。
我在 Excel 中找到了可能的解决方案,但我已经很长时间没有使用 VBA,并且无法对这个实现做出正面或负面的评价。
这些功能似乎解决了我的问题,尽管可能需要测试更远的年份:
// 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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句