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

相关推荐
TE-茶叶蛋2 小时前
ThinkPHP入门
php
TE-茶叶蛋3 小时前
结合登录页-PHP基础知识点解析
android·开发语言·php
流觞 无依4 小时前
DedeCMS plus/comment.php 评论 XSS/注入(XSS、SQL注入)修复教程
sql·php·xss
cyber_两只龙宝6 小时前
【Nginx】Nginx实现FastCGI详解
linux·运维·nginx·云原生·php·memcached·fastcgi
dog2506 小时前
卡瓦列里积分赏析
开发语言·php
5G丶6 小时前
ThinkPHP 集群部署完整指南
数据库·php
流觞 无依6 小时前
DedeCMS plus/recommend.php SQL注入漏洞修复教程
sql·php
流觞 无依7 小时前
DedeCMS plus/list.php 参数注入(SQL注入)修复教程
sql·安全·php
软件开发技术7 小时前
最新网络游戏账户交易平台系统源码 全开源版本 全新UI 自适应移动端
php
酿情师7 小时前
PHP 反序列化漏洞与 POP 链详解:网络安全小白从零入门
android·web安全·php