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

相关推荐
niucloud-admin9 分钟前
PHP SAAS 框架常见问题——服务器请求超时问题
服务器·php
HookJames16 分钟前
让 FlyingPress 的 Preload 队列变少,减轻 PHP 和数据库压力
android·数据库·php
Sayuanni%328 分钟前
五层网络模型-应用层
开发语言·网络·php
zopple12 小时前
ThinkPHP5常见问题及解决方案
python·php·laravel
RNEA ESIO13 小时前
PHP进阶-在Ubuntu上搭建LAMP环境教程
开发语言·ubuntu·php
zopple14 小时前
Laravel vs ThinkPHP:PHP框架终极对决
开发语言·php·laravel
常利兵19 小时前
大文件上传不再卡顿:Spring Boot 分片上传、断点续传与进度条实现全解析
spring boot·后端·php
念何架构之路19 小时前
图解常见网络I/O复用模型
服务器·网络·php
@insist12320 小时前
网络工程师-实战配置篇(一):深入 BGP 与 VRRP,构建高可靠网络
服务器·网络·php·网络工程师·软件水平考试
黑牛儿1 天前
PHP 8.3性能暴涨实测|对比8.2,接口响应提速30%,配置无需大幅修改
android·开发语言·后端·php