php strtotime常见用法

strtotime() 函数在 PHP 中用于将日期时间字符串转换为 UNIX 时间戳,它可以解析包括英文描述的日期时间字符串。以下是 strtotime() 函数的一些常见用法:

日期时间字符串转换为 UNIX 时间戳:

php 复制代码
$timestamp = strtotime('2024-04-20 10:30:00');

相对时间转换为 UNIX 时间戳:

php 复制代码
// 当前时间的前一天
$timestamp = strtotime('-1 day');
// 当前时间的后一周
$timestamp = strtotime('+1 week');

解析英文日期时间描述:

php 复制代码
// 下周五
$timestamp = strtotime('next Friday');
// 2小时后
$timestamp = strtotime('+2 hours');
// 上个月的第一天
$timestamp = strtotime('first day of last month');

解析相对日期时间描述:

php 复制代码
// 3天后的下午3点
$timestamp = strtotime('3 days 15:00');
// 明年的1月1日
$timestamp = strtotime('next year January 1');

指定基准时间进行解析:

php 复制代码
// 在指定时间基础上加上3天
$timestamp = strtotime('+3 days', $baseTimestamp);

这些都是 strtotime() 函数常见的用法,可以根据具体需求选择合适的方式来进行日期时间字符串的解析。

相关推荐
北辰当尹1 天前
第27天 安全开发-PHP应用&TP框架&路由访问&对象操作&内置过滤绕过&核心漏洞
android·安全·php
edisao1 天前
四。SpaceX、网络化与未来的跨越:低成本、高频次的真正威胁
大数据·开发语言·人工智能·科技·php
小小代码狗1 天前
PHP伪协议和文件包含
网络·网络安全·php
qq_406176141 天前
深耕JS防抖与节流:从原理到工程化实践的全方位指南
开发语言·php
吉吉611 天前
在 Windows 和 Linux 的 VSCode 中配置 PHP Debug
开发语言·php
catchadmin1 天前
PHP 8.5 闭包和一等可调用对象进入常量表达式
php
JaguarJack1 天前
2026 年 PHP 函数式编程 优势与实际应用
后端·php·服务端
zorro_z1 天前
ThinkPHP8学习篇(十五):验证
php
wr2005141 天前
渗透笔记和疑惑
开发语言·php
Y_cheng_1 天前
php环境配置与伪协议
开发语言·php