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

相关推荐
Ssan PRIN30 分钟前
Modbus报文详解
服务器·开发语言·php
小草cys1 小时前
review20260404
开发语言·php
sensen_kiss2 小时前
CAN302 Technologies for E-Commerce 电子商务技术 Pt.4 深入了解PHP和SQL
开发语言·sql·php
jwn9992 小时前
PHP vs 汇编:编程语言的两极对决
开发语言·汇编·php
取码网13 小时前
帝国CMS作文网题目文学文章wap+pc自适应响应式模板PHP网站源码
php
arron889914 小时前
RK3588 Ubuntu 22.04 静态 IP 真实配置文件位置 + 最稳修改方法
tcp/ip·ubuntu·php
温天仁19 小时前
西门子PLC编程实践教程:工控工程案例学习
开发语言·学习·自动化·php
小草cys1 天前
review202604032342
开发语言·php
桌面运维家1 天前
交换机环路排查:STP配置实战与网络故障精确定位
开发语言·php
广州灵眸科技有限公司1 天前
瑞芯微(EASY EAI)RV1126B 人脸98关键点算法识别
开发语言·科技·嵌入式硬件·物联网·算法·php