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 分钟前
sqlmap的使用
sql·网络安全·php
roman_日积跬步-终至千里1 小时前
【Starrocks】StarRocks 排错:`Invalid method name: ‘heartbeat‘`(BE 心跳端口/协议错误)
服务器·网络·php
Maybe I Simple1 小时前
PHP常用方法封装
php
slongzhang_1 小时前
PHP图片处理|画布入门
开发语言·php
知码者2 小时前
对于Thinkphp5可能遇到的保存问题
服务器·php·apache·小程序开发·跨平台小程序
郑州光合科技余经理2 小时前
实战分享:如何构建东南亚高并发跑腿配送系统
java·开发语言·javascript·spring cloud·uni-app·c#·php
云云只是个程序马喽2 小时前
海外短剧系统开发核心功能设计及上线材料准备
小程序·php
一往无前fgs3 小时前
PHP语言开发基础入门实践教程(零基础版)
开发语言·php
步步为营DotNet5 小时前
深度剖析.NET中HttpClient的请求重试机制:可靠性提升与实践优化
开发语言·php·.net
JaguarJack5 小时前
再推荐 10 个低调但非常实用的 PHP 包
后端·php