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

相关推荐
Xudde.1 小时前
班级作业笔记报告0x10
笔记·学习·安全·web安全·php
我叫黑大帅3 小时前
PHP中的官方操作数据库PDO
后端·面试·php
Zzj_tju3 小时前
Java 从入门到精通(十二):File 与 IO 流基础,为什么程序“读写文件”时总是容易出问题?
java·python·php
流觞 无依6 小时前
DedeCMS 前台任意用户密码修改漏洞(CNVD-2018-0109)修复教程
php·dede漏洞
枫叶林FYL8 小时前
【自然语言处理 NLP】前沿架构与多模态 6.1.2 专家混合模型(Mixture of Experts, MoE)
开发语言·php
电商API&Tina8 小时前
跨境电商如何接入1688官方寻源通接口?附接入流程
java·数据库·python·sql·oracle·json·php
jwn9998 小时前
Laravel11.x新特性全解析
android·开发语言·php·laravel
CDN3609 小时前
弱网下游戏盾掉线重连失败?链路保活与超时参数优化
开发语言·游戏·php
cch89189 小时前
PHP爬虫框架大比拼
开发语言·爬虫·php
jwn9999 小时前
Laravel 8.x新特性全解析
php·laravel