PHP时间处理完全指南:日期获取、格式化与计算(实战代码示例)
54 阅读量
发布时间:20天前
PHP时间处理完全指南,详解time()、date()函数使用与DateTime类操作,包含日期格式化方法、时间戳转换及昨天/明天、上周/下周等相对时间计算实战。掌握PHP开发必备的时间处理技巧,提升日期操作效率。
在 PHP 开发中,精准获取和灵活处理时间是常见需求。PHP 提供了丰富的函数和方法,包括当前时间、相对时间(如昨天、明天、上周、下月等)的获取与计算。以下是全面的时间获取方法:
基础时间获取
1. 使用time()
函数:获取 Unix 时间戳
$timestamp = time(); // 返回从 1970-01-01 00:00:00 UTC 至今的秒数
echo '当前时间戳:' . $timestamp; // 示例输出:1720800000
2. 使用microtime
函数:获取带微秒的时间
$microtime = microtime(true); // 浮点数格式,如 1720800000.1234
$microtime_str = microtime(); // 字符串格式,如 "0.12345600 1720800000"
3. 使用date()
函数:格式化日期时间
// 基本格式
$currentTime = date('Y-m-d H:i:s');
echo '当前时间:' . $currentTime; // 示例输出:2025-07-12 15:30:45
// 自定义格式
$formatted = date('l, F j, Y', time());
echo '完整格式:' . $formatted; // 示例输出:Saturday, July 12, 2025
// 其他常用格式
echo date('Y/m/d'); // 输出:2025/07/12
echo date('d-m-Y'); // 输出:12-07-2025
echo date('Y年m月d日'); // 输出:2025年07月12日
✅ 日期格式字符说明:
格式字符 | 说明 | 示例 |
---|---|---|
Y |
4位年份 | 2023 |
y |
2位年份 | 23 |
m |
月份(带前导零) | 01-12 |
n |
月份(不带前导零) | 1-12 |
d |
日期(带前导零) | 01-31 |
j |
日期(不带前导零) | 1-31 |
H |
24小时制(带前导零) | 00-23 |
G |
24小时制(不带前导零) | 0-23 |
i |
分钟(带前导零) | 00-59 |
s |
秒数(带前导零) | 00-59 |
l |
星期几全名 | Monday |
D |
星期几缩写 | Mon |
F |
月份全名 | January |
M |
月份缩写 | Jan |
相对时间计算
1. 昨天、今天、明天
// 昨天
$yesterday = date('Y-m-d', strtotime('-1 day'));
// 或使用 DateTime
$yesterday = (new DateTime('-1 day'))->format('Y-m-d');
// 今天
$today = date('Y-m-d');
// 明天
$tomorrow = date('Y-m-d', strtotime('+1 day'));
2. 上周、本周、下周
// 上周
$lastMonday = date('Y-m-d', strtotime('last monday'));
$lastSunday = date('Y-m-d', strtotime('last sunday'));
// 本周
$thisMonday = date('Y-m-d', strtotime('this week'));
$thisSunday = date('Y-m-d', strtotime('this week +6 days'));
// 下周
$nextMonday = date('Y-m-d', strtotime('next monday'));
$nextSunday = date('Y-m-d', strtotime('next sunday'));
3. 上个月、本月、下个月
// 上个月
$firstDayLastMonth = date('Y-m-01', strtotime('-1 month'));
$lastDayLastMonth = date('Y-m-t', strtotime('-1 month'));
// 本月
$firstDayThisMonth = date('Y-m-01');
$lastDayThisMonth = date('Y-m-t');
// 下个月
$firstDayNextMonth = date('Y-m-01', strtotime('+1 month'));
$lastDayNextMonth = date('Y-m-t', strtotime('+1 month'));
4. 去年、今年、明年
// 去年
$lastYear = date('Y', strtotime('-1 year'));
// 今年
$thisYear = date('Y');
// 明年
$nextYear = date('Y', strtotime('+1 year'));
高级时间操作
1. 使用 DateTime 类
// 创建 DateTime 对象
$now = new DateTime();
$specificDate = new DateTime('2025-07-15');
// 修改日期
$date = new DateTime();
$date->modify('+1 day');
echo $date->format('Y-m-d'); // 输出明天的日期
// 计算日期差
$date1 = new DateTime('2025-07-01');
$date2 = new DateTime('2025-07-12');
$interval = $date1->diff($date2);
echo $interval->days; // 输出 11
2. 时区设置
// 设置默认时区
date_default_timezone_set('Asia/Shanghai');
// 获取当前时区
echo date_default_timezone_get();
// 使用时区创建 DateTime
$date = new DateTime('now', new DateTimeZone('America/New_York'));
3. 其他实用函数
// 检查日期有效性
var_dump(checkdate(2, 29, 2024)); // 返回 true,2024是闰年
var_dump(checkdate(2, 29, 2025)); // 返回 false
// 获取某个月的天数
echo cal_days_in_month(CAL_GREGORIAN, 2, 2024); // 输出 29
// 获取当前周数
echo date('W'); // 输出当前是一年中的第几周
实际应用示例
1. 计算年龄
function calculateAge($birthdate) {
$birthday = new DateTime($birthdate);
$today = new DateTime();
$age = $today->diff($birthday);
return $age->y;
}
echo calculateAge('1990-05-15'); // 输出实际年龄
2. 工作日计算
function addBusinessDays($startDate, $businessDays) {
$date = new DateTime($startDate);
$addedDays = 0;
while ($addedDays < $businessDays) {
$date->modify('+1 day');
$dayOfWeek = $date->format('N');
if ($dayOfWeek < 6) { // 1-5 是工作日
$addedDays++;
}
}
return $date->format('Y-m-d');
}
echo addBusinessDays('2025-07-12', 5); // 计算5个工作日后的日期
通过掌握这些时间处理技巧,你可以轻松应对 PHP 开发中的各种时间相关需求。
热门文章
-
PHP时间处理完全指南:日期获取、格式化与计算(实战代码示例)
54 2025-07-12
-
MySQL 分词搜索实现方案:全文索引 vs 自定义分词
68 2025-07-12
最新文章
找到专属于你的技术圈子
申请回复「进群」加入官方微信群
