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

相关推荐
石榴树下的七彩鱼10 小时前
图片修复 API 接入实战:网站如何自动去除图片水印(Python / PHP / C# 示例)
图像处理·后端·python·c#·php·api·图片去水印
Chockmans12 小时前
春秋云境CVE-2021-34257
安全·web安全·网络安全·php·网络攻击模型·春秋云境·cve-2021-34257
盛世宏博北京13 小时前
以太网温湿度传感器运维技巧,提升设备稳定性与使用寿命
开发语言·php·以太网温湿度传感器
aq553560018 小时前
ThinkPHP5.x核心特性全解析
android·数据库·oracle·php·laravel
KevinCyao19 小时前
php彩信接口代码示例:PHP使用cURL调用彩信网关发送图文消息
android·开发语言·php
其实防守也摸鱼20 小时前
集成开发环境phpStudy安装与配置指南(包含DVWA)
网络·安全·php·web·ctf·工具配置
Cyber4K20 小时前
【Nginx专项】高级进阶架构篇-Proxy正反向代理、FastCGI及PHP-FPM介绍
运维·服务器·nginx·架构·php
传说中胖子21 小时前
Magento服务器VSCode开启XDebug方法
服务器·vscode·php
niucloud-admin1 天前
PHP SAAS 框架常见问题——配置问题——小程序消息推送配置 Token 校验失败
php
FreeBuf_1 天前
微软 SharePoint Server 0Day漏洞遭在野利用
microsoft·php·sharepoint