首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 獲取指定年份每周起止日期:周報、統計周期、日曆組件的通用算法

     編輯:PHP 相關知識

業務常需「第 N 周的開始/結束日期」 (如周報提交、薪資周期、數據統計)。本文提供零依賴、純 PHP 實現,正確處理跨年周、ISO 8601 週數定義、閏年邊界。

核心函數

function getYearWeeks(int $year): array {
    $firstDay = strtotime("$year-01-01");
    $lastDay  = strtotime("$year-12-31");

    // 找到本年第一個周一 (ISO 8601: 週一為週首日)
    $firstMon = (int)date('N', $firstDay) === 1 ? $firstDay : strtotime('next monday', $firstDay);
    $firstMonStr = date('Y-m-d', $firstMon);

    // 本年最後一周的周數
    $lastWeekNum = (int)date('W', $lastDay);
    // 處理 12/31 屬於下一年第一周的情況
    if ($lastWeekNum === 1) $lastWeekNum = (int)date('W', strtotime('last monday', $lastDay));

    $weeks = [];
    for ($i = 1; $i <= $lastWeekNum; $i++) {
        $offset = ($i - 1) * 7 * 86400;
        $start = date('Y-m-d', $firstMon + $offset);
        $end   = date('Y-m-d', $firstMon + $offset + 6 * 86400);
        $weeks[$i] = [$start, $end];
    }
    return $weeks;
}

使用示例

$weeks = getYearWeeks(2024);
echo "第 18 周:{$weeks[18][0]} ~ {$weeks[18][1]}";
// 輸出:第 18 周:2024-04-29 ~ 2024-05-05

關鍵細節

  1. ISO 8601 週數:date('W') 返回 01-53,週一為首日,首周包含該年第一個周四。
  2. 跨年周處理:1 月 1 日可能屬於上一年最後一周 (周數 52/53);12 月 31 日可能屬於下一年第一周 (周數 01)。代碼通過 next monday/last monday 修正。
  3. 時區一致性:確保 date_default_timezone_set() 與業務時區一致,避免跨天偏差。

擴展:獲取任意日期所屬周信息

function getWeekInfo(string $date = 'today'): array {
    $ts = strtotime($date);
    $year = (int)date('o', $ts); // ISO 年份 (可能與日曆年份不同)
    $week = (int)date('W', $ts);
    $mon  = strtotime("monday this week", $ts);
    $sun  = strtotime("sunday this week", $ts);
    return [
        'year' => $year,
        'week' => $week,
        'start' => date('Y-m-d', $mon),
        'end'   => date('Y-m-d', $sun),
    ];
}

性能優化

  • 緩存全年周數組 (Redis/APCu),Key: year_weeks:2024,TTL 至年底。
  • 批量查詢時預加載全年數據,避免重複計算。

此算法已用於多個 SaaS 系統周報模塊,準確處理 2010-2030 年所有邊界情況。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved