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

相关推荐
南 阳12 小时前
Python从入门到精通day59
开发语言·python·php
源码潇潇和逸逸13 小时前
独立部署高校圈子平台:PHP+UniApp打造社交+交易+服务一站式校园解决方案
开发语言·uni-app·php
liuxin_072517 小时前
vscode cursor
php
桌面运维家17 小时前
KVM虚拟机:Neutron网络故障诊断与修复实战
服务器·网络·php
hzb6666618 小时前
xd_day32-day40
java·javascript·学习·安全·web安全·tomcat·php
思茂信息19 小时前
CST软件加载 Pin 二极管的可重构电桥仿真研究
服务器·开发语言·人工智能·php·cst·电磁仿真·电磁辐射
徒 花20 小时前
HCIA知识整理2
开发语言·php
大黄说说21 小时前
PHP 数组 vs SPL 数据结构:队列与栈场景下的性能对决
开发语言·数据结构·php
一只会跑会跳会发疯的猴子21 小时前
php操作mysql数据库增删改查
数据库·mysql·php
luanma1509801 天前
PHP vs Lua:脚本语言核心差异解析
开发语言·php·lua