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() 函数常见的用法,可以根据具体需求选择合适的方式来进行日期时间字符串的解析。

相关推荐
CV工程师丁Sir23 分钟前
ArkWeb 手记 06|window.open 与页面跳转拦截
开发语言·php·harmonyos
可乐鸡翅yeah_37 分钟前
HLS 业务 Referer‑Policy 网页元标签引发播放异常排错
前端·javascript·网络·ffmpeg·php·音视频
vipxieliang1 小时前
PHP 依赖注入容器从零实现:控制反转、手动注入与容器管理全解析
后端·php
运维行者_1 小时前
PHP性能监控怎么做?从响应时间到慢函数的6个关键指标
运维·服务器·开发语言·网络·支持向量机·php·接口隔离原则
Do_It_Today2 小时前
php xml 入门学习资料
开发语言·php
Do_It_Today3 小时前
php url路由入门实例
开发语言·php
fundoit3 小时前
资源服务器如何对 JWT 进行验签
java·运维·服务器·spring boot·php·oauth2
专业程序开发源3 小时前
SSM笔记本在线销售系统32649-计算机课程设计、毕业设计
java·spring boot·后端·python·django·php·课程设计
行业研究员4 小时前
AI反电诈怎么做?腾讯云天御风控Agent方案与落地解析
人工智能·php·腾讯云·ai反诈