PHP获取特殊时间范围(今天/昨天/本周/本月/本季/本年)起止时间戳的方法整理
在 Web 开发中,按特定时间节点进行数据查询非常普遍。本文系统整理了使用 PHP 计算今天、昨天、本周、本月、本季度及本年度起止时间戳的多种实现方式,并提供封装好的工具函数,助您快速构建时间范围筛选逻辑。
1. 当天时间戳与日期格式
获取当天的起止时间,最直观的方式是利用 date() 格式化当前日期后拼接字符串,再通过 strtotime() 转换。
$startTime = strtotime(date('Y-m-d') . '00:00:00');
$overTime = strtotime(date('Y-m-d') . '23:59:59');
若需要直接获取日期格式的字符串,同样可以基于格式化后的日期进行拼接:
// 注意:弱类型语言中字符串拼接需保证格式一致性
$startDate = date('Y-m-d') . ' 00:00:00';
$overDate = date('Y-m-d') . ' 23:59:59';
2. 昨天时间戳与日期格式
昨天的时间跨度计算可以通过调整日期参数或直接利用 strtotime('-1 day') 获取。
方法一:使用 mktime 调整日期
通过修改日期的‘天’部分,或获取今日零点前减一秒来实现:
// 昨日00:00:00
$startTime = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
// 今日00:00:00 前减1秒即为昨日的最后一秒
$overTime = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
方法二:时间戳转日期格式
得到上述时间戳后,直接使用 date() 转换即可:
$startDate = date("Y-m-d H:i:s", $startTime);
$overDate = date("Y-m-d H:i:s", $overTime);
方法三:动态计算天数(注意月初边界问题)
尝试直接用 date('d') - 1 作为日期值存在隐患,当日期为每月1号时,会计算出“第0天”,导致结果异常。因此推荐优先使用前两种更稳健的方法。
3. 本周时间戳与日期格式
PHP 的 date('w') 可返回今天是周几(0表示周日,1-6表示周一至周六)。基于此可以推算本周的起止点。
方法一:通过 mktime 偏移计算
// 假设每周从周一开始算(根据业务需求调整),此处以周日为起点简化说明,或手动修正
// 以下为基于 date('w') 的传统算法,通常需结合具体周的起始定义
$startTime = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('y'));
$overTime = mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('y'));
方法二:利用 relative formats(推荐)
这种方法更为简洁,但需注意 strtotime() 对“本周第一天”的定义可能因地区设置而异:
$nowDate = date("Y-m-d");
$week = date('w', strtotime($nowDate));
// 计算距离当前日期过去的天数,使结果为本周第一天
$startTime = strtotime("$nowDate -" . ($week ? $week - 1 : 6) . ' days');
$overTime = $startTime + 86400 * 7 - 1;
4. 本月时间戳与日期格式
每月的天数不固定,PHP 提供了 date('t') 来获取当月最后一天的数字,这使得本月时间的计算非常精确。
// 本月起始时间戳 (当月1号 00:00:00)
$startTime = mktime(0, 0, 0, date('m'), 1, date('Y'));
// 本月结束时间戳 (当月最后一天 23:59:59)
$overTime = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
// 对应日期字符串
$startDate = date("Y-m-d H:i:s", $startTime);
$overDate = date("Y-m-d H:i:s", $overTime);
5. 本季度时间戳与日期格式
季度划分依据月份除以 3 向上取整。每个季度的首月开始于 (季度数-1)*3 + 1。
// 获取当前季度 (1, 2, 3, 4)
$season = ceil((date('m')) / 3);
// 当前季度第一个月是 season*3 - 2
// 例如 Q1: 1*3-2=1, Q2: 2*3-2=4, Q3: 3*3-2=7
$startMonth = $season * 3 - 2;
$startTime = mktime(0, 0, 0, $startMonth, 1, date('Y'));
// 季度最后一个月是 season*3
$endMonth = $season * 3;
// 获取该月最后一天
$daysInEndMonth = date('t', mktime(0, 0, 0, $endMonth, 1, date('Y')));
$overTime = mktime(23, 59, 59, $endMonth, $daysInEndMonth, date('Y'));
// 转换为日期格式
$dateStart = date("Y-m-d H:i:s", $startTime);
$dateEnd = date("Y-m-d H:i:s", $overTime);
6. 本年时间戳与日期格式
本年度的起始和结束相对固定,直接锁定 1月1日 和 12月31日 即可。
$startTime = strtotime(date("Y", time()) . "-1-1");
$overTime = strtotime(date("Y", time()) . "-12-31") + 86399; // 或者用 mktime 设为 23:59:59
// 或者更严谨地包含时分秒
$startTime = mktime(0, 0, 0, 1, 1, date('Y'));
$overTime = mktime(23, 59, 59, 12, 31, date('Y'));
7. 快捷描述符与通用转换
在处理灵活的时间段(如明天、下周、下个月等)时,strtotime() 支持自然语言描述符:
strtotime("+1 day"):明天的此时时间戳strtotime("-1 week"):上周此时的时间戳strtotime("next Thursday"):下一个周四此时的时间戳strtotime("last month"):上个月此时的时间戳
基础转换对照:
- 时间戳转日期:
date("Y-m-d H:i:s", $timestamp) - 日期转时间戳:
strtotime("2023-10-01 12:00:00")
8. 综合工具函数封装
为了方便在实际项目中复用,可以将常见的时间节点封装为一个函数。以下是一个处理了今天、昨天、前天、本周、本月、上月、去年等场景的基础模板(注:具体业务对“本周/本月”结束的界定可能不同,可根据需求微调逻辑):
function getTimeRangeByLabel($label) {
switch ($label) {
case 'today':
return [
'start' => strtotime(date('Y-m-d')),
'end' => strtotime(date('Y-m-d', strtotime('+1 day'))) - 1
];
case 'yesterday':
return [
'start' => strtotime(date('Y-m-d', strtotime('-1 day'))),
'end' => strtotime(date('Y-m-d')) - 1
];
case 'this_week':
// 以周一为一周的开始为例
$monday = date('Y-m-d', strtotime('this week Monday'));
$sunday = date('Y-m-d', strtotime('this week Sunday'));
return [
'start' => strtotime($monday . ' 00:00:00'),
'end' => strtotime($sunday . ' 23:59:59')
];
case 'this_month':
return [
'start' => strtotime(date('Y-m-01')),
'end' => strtotime(date('Y-m-t')) // t为当月最后一天
];
case 'last_month':
return [
'start' => strtotime(date('Y-m-01', strtotime('-1 month'))),
'end' => strtotime(date('Y-m-01')) - 1
];
default:
// 默认返回今天
return [
'start' => strtotime(date('Y-m-d')),
'end' => strtotime(date('Y-m-d', strtotime('+1 day'))) - 1
];
}
}
以上就是PHP获取特殊时间戳与时间范围详细整理的过程,更多关于PHP时间处理优化的资料请关注本站其它相关文章!
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









