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

PHP 日期運算最佳實踐:取得本月、上月、下月首尾日期 (strtotime 相對格式完全指南)

     編輯:PHP 相關知識

核心技巧:strtotime 相對日期格式

strtotime() 支援極豐富的英文相對描述,可直接處理月份邊界、閏年、跨年等複雜邏輯,無需手動判斷 12→1 月轉換。

標準函數庫

<?php
/**
 * 取得指定月份的第一天 00:00:00 與最後一天 23:59:59
 * @param string $date 任意可被 strtotime 解析的日期字串 (如 '2024-03', '2024-03-15', 'first day of this month')
 * @return array [$startDateTime, $endDateTime] 格式 'Y-m-d H:i:s'
 */
function getMonthRange(string $date): array {
    $first = date('Y-m-01 00:00:00', strtotime($date));
    $last  = date('Y-m-t 23:59:59', strtotime($first)); // t = 該月總天數
    return [$first, $last];
}

/** 取得上月範圍 */
function getLastMonthRange(string $date = 'now'): array {
    $first = date('Y-m-01 00:00:00', strtotime('first day of last month', strtotime($date)));
    $last  = date('Y-m-t 23:59:59', strtotime($first));
    return [$first, $last];
}

/** 取得下月範圍 */
function getNextMonthRange(string $date = 'now'): array {
    $first = date('Y-m-01 00:00:00', strtotime('first day of next month', strtotime($date)));
    $last  = date('Y-m-t 23:59:59', strtotime($first));
    return [$first, $last];
}

/** 取得任意月份偏移 */
function getMonthOffsetRange(int $offset, string $base = 'now'): array {
    $first = date('Y-m-01 00:00:00', strtotime("first day of $offset month", strtotime($base)));
    $last  = date('Y-m-t 23:59:59', strtotime($first));
    return [$first, $last];
}

// 使用範例
[$start, $end] = getMonthRange('2024-02-10');      // 本月
[$start, $end] = getLastMonthRange();              // 上月
[$start, $end] = getNextMonthRange();              // 下月
[$start, $end] = getMonthOffsetRange(-3);          // 3 個月前
[$start, $end] = getMonthOffsetRange(2, '2024-01-31'); // 2024-01 起算 2 個月後 (正確處理 1/31 → 3/31)
echo "開始: $start\n結束: $end\n";

關鍵相對格式速查表

格式說明範例
first day of this month本月 1 號 00:002024-03-01 00:00:00
last day of this month本月最後一天 23:59:59 (配合 date t)2024-03-31 23:59:59
first day of last month上月 1 號自動跨年 (1月→去年12月)
first day of next month下月 1 號自動跨年 (12月→明年1月)
+1 month / -1 month月份偏移 (注意: 1/31 +1 month → 3/3 非 2/31)配合 first day of 更安全
last day of +2 months兩個月後的月底直接取得月末

DateTime 物件導向寫法 (推薦新專案)

$dt = new DateTime('2024-02-15');
$dt->modify('first day of this month')->setTime(0,0,0);
$start = $dt->format('Y-m-d H:i:s');
$dt->modify('last day of this month')->setTime(23,59,59);
$end = $dt->format('Y-m-d H:i:s');
// 可鏈式操作、支援時區、不可變版本 DateTimeImmutable

常見陷阱

  • +1 month 陷阱:strtotime('2024-01-31 +1 month') 會得到 2024-03-02 (因 2 月無 31 日),務必改用 first day of next month。
  • 時區不一致:date_default_timezone_set('Asia/Taipei') 應在 bootstrap 統一設定,避免跨伺服器部署結果不同。
  • date('t') 回傳字串,參與運算時請 (int) 轉型。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved