使用strtotime()的'first day this month'返回上月,求原因及方案
PHP strtotime()相对日期计算异常的解决办法
你碰到的不是strtotime()的bug,而是它对相对日期字符串的解析逻辑存在歧义,尤其是在处理边界时间戳时容易出问题。先看你用的时间戳1677628800,转成具体日期是2023-03-01 00:00:00(默认时区下),当用last day this month这类格式时,strtotime的模糊解析逻辑可能因为时区偏移或日期边界处理出现偏差。
下面给你两种更可靠的解决方案:
优先推荐:用DateTime类处理
DateTime是PHP官方推荐的日期处理类,逻辑清晰,对相对日期的解析比strtotime精准得多。针对你的场景,修改代码如下:
<?php // 用时间戳初始化DateTime,建议显式指定时区避免问题 $targetDate = DateTime::createFromFormat('U', '1677628800', new DateTimeZone('UTC')); // 获取当月最后一天 $thisMonthLastDay = clone $targetDate; $thisMonthLastDay->modify('last day of this month'); echo "Last day of " . $targetDate->format('M-Y') . " is: " . $thisMonthLastDay->format('M-d-Y') . " (expecting March 31!) <br>"; // 获取下月最后一天 $nextMonthLastDay = clone $targetDate; $nextMonthLastDay->modify('last day of next month'); echo "Last day of " . $targetDate->format('M-Y') . " is: " . $nextMonthLastDay->format('M-d-Y') . " (expecting April 30!) <br>"; // 获取下月第一天 $nextMonthFirstDay = clone $targetDate; $nextMonthFirstDay->modify('first day of next month'); echo "First day of next month from " . $targetDate->format('M-Y') . " is: " . $nextMonthFirstDay->format('M-d-Y') . " (expecting first day of April) <br>"; // 基于当月第一天取当月最后一天 $currentFirstDay = (new DateTime())->modify('first day of this month'); $currentLastDay = clone $currentFirstDay; $currentLastDay->modify('last day of this month'); echo "Last day of " . $currentFirstDay->format('M-Y') . " is: " . $currentLastDay->format('M-d-Y') . "<br>"; ?>
DateTime的优势在于:
modify方法对last day of this month这类带of的格式解析更严谨,不会出现模糊匹配的问题。- 可以显式指定时区,彻底避免服务器时区设置导致的日期偏差。
- 支持对象克隆,操作时不会修改原日期对象,适合多步骤的日期计算。
若坚持用strtotime的优化方案
如果一定要用strtotime,建议先把时间戳转成当月的中间时间点(比如12点),避开凌晨的时区边界问题:
<?php // 先获取指定时间戳对应当月1号12点的时间戳 $midMonthTimestamp = strtotime(date('Y-m-01 12:00:00', 1677628800)); echo "Last day of " . date('M-Y', 1677628800) . " is: " . date('M-d-Y', strtotime('last day this month', $midMonthTimestamp)) . " (expecting March 31!) <br>"; ?>
选12点而非0点,是为了防止时区偏移导致日期意外跳转到上月或下月。
内容的提问来源于stack exchange,提问作者Switchboy
相关产品推荐
相关产品推荐

