PHP如何为周四日期显示逻辑添加周三17点时间截断规则
PHP日期逻辑调整方案
核心修改点
- 显式指定GMT+1/CEST对应的时区,自动适配冬夏令时切换,避免服务器默认时区偏差导致判断错误
- 拆分周三的判断逻辑,新增17点的时间截断规则,其余时段保持原有逻辑不变
调整后完整代码
<?php // 指定GMT+1/CEST时区,自动适配冬/夏令时,也可替换为'Europe/Paris'、'Europe/Berlin'等同时区标识 $target_timezone = new DateTimeZone('Europe/Amsterdam'); $current_date = new DateTime('now', $target_timezone); $weekday = $current_date->format('D'); $hour = (int)$current_date->format('G'); if ($weekday === 'Tue') { // 周二保持原有逻辑,取下一周周四 $current_date->modify('thursday next week'); } elseif ($weekday === 'Wed') { if ($hour < 17) { // 周三17点前,显示次日(本周四) $current_date->modify('next thursday'); } else { // 周三17点后,显示下一周周四 $current_date->modify('thursday next week'); } } else { // 其余时段保持原有逻辑,显示即将到来的周四 $current_date->modify('next thursday'); } $delivery_date = $current_date->format('d-m-Y'); ?> <?php echo $delivery_date; ?>
逻辑说明
所有时间判断统一使用同一个带时区的DateTime实例,避免多次调用原生date()函数可能出现的毫秒级时间差导致的判断异常,完全符合规则要求。
内容的提问来源于stack exchange,提问作者Erwin van Ekeren
相关产品推荐
相关产品推荐

