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

相关推荐
星光一影1 小时前
供应链进销存源码uniapp全开源ERP多仓库管理系统pc+app手机端
mysql·elementui·uni-app·开源·php·phpstorm·1024程序员节
行思理4 小时前
Dockerfile 各指令说明
运维·macos·docker·容器·php
格拉格拉8 小时前
PHP基础知识
开发语言·php·php基础
鹿鸣天涯11 小时前
网络安全等级保护测评高风险判定实施指引(试行)--2020与2025版对比
开发语言·php
贝西奇谈12 小时前
JavaScript DOM节点操作详解
开发语言·javascript·php
报错小能手12 小时前
计算机网络自顶向下方法4——详解协议层次及其服务模型
服务器·计算机网络·php
光影341513 小时前
专利撰写与申请核心要点简报
前端·数据库·php
应用市场13 小时前
PHP microtime()函数精度问题深度解析与解决方案
android·开发语言·php
没有故事、有酒14 小时前
Axios
开发语言·php
BingoGo14 小时前
Laravel 新项目避坑指南10 大基础设置让代码半年不崩
后端·php