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

相关推荐
寺中人18 小时前
Xshell 完全入门指南:从安装到实战,远程连接+文件传输+会话管理全拆解
git·ssh·github·php·远程连接·xshell·运维工具
hengdonghui20 小时前
Writeup 4 津门杯 2021 Web hate_php
php·web·ctf·通配符
现任明教教主~21 小时前
企业查询系统源码
php
派小心.21 小时前
页面关闭前埋点丢失:sendBeacon验收
开发语言·php
我就是不信1 天前
TCP 套接字中的 I/O 缓冲:原理、机制与调优实践
网络·tcp/ip·php
张小姐的猫1 天前
【Linux】网络编程 —— 五种IO模型
linux·运维·服务器·网络·c++·人工智能·php
ESDWAN1 天前
跨境电商网络专线怎么选?从带宽、延迟到SD-WAN部署与带宽管理的完整落地指南
开发语言·网络·php
qetfw1 天前
Debian iSCSI Target 与 open-iscsi:LVM 后端、ACL 与客户端验证
linux·服务器·debian·php
刘胡子大叔1 天前
PHP 扩展加载失败
php
一木 之林1 天前
阿里云百炼与通义千问接入
开发语言·php