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 小时前
无线网络安全--10 规避WLAN验证之挫败MAC地址限制
网络·智能路由器·php·教程·虚拟机·wlan·无线网络安全
浩风祭月3 小时前
我用 AI 辅助重构了遗留项目的认证模块:从明文存储到 OAuth 2.0 的安全升级
后端·php·ai编程
宋拾壹3 小时前
fastadmin列表中查看列表,并且添加增加相应的数据
javascript·php·fastadmin
weixin_446260854 小时前
LLM智能体在社交模拟中的决策行为分析:有限状态与LLM-based策略对比研究
开发语言·php
唐青枫6 小时前
Php Doctrine ORM 实战详解:从实体映射到查询、关联与事务
php·symfony
一直奔跑在路上7 小时前
深入浅出RDMA:原理、应用与实战指南
开发语言·php
右耳朵猫AI8 小时前
PHP周刊2026W23 | Composer 2.10、Symfony 8.1、Twig 3.27.1、PHP 8.5、Laravel AI SDK
php·composer·symfony
2401_8346369919 小时前
Nginx 从入门到实战:静态 / 动态站点、PHP 部署与反向代理全解析
运维·nginx·php
绵绵细雨中的乡音1 天前
监控显示一切正常,可用户根本打不开网站——Blackbox Exporter帮我找到了真相(1)
开发语言·php
右耳朵猫AI1 天前
PHP周刊2026W22 | WordPress 7.0发布、Laravel 13.10.0、Polyfill 1.38.1、Symfony 8.1
php·laravel·symfony