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

相关推荐
北漂Zachary7 小时前
Laravel 10.x 新特性全面解析
php·laravel
2401_8274999914 小时前
python项目实战10-网络机器人03
开发语言·python·php
xinhuanjieyi16 小时前
php setplayersjson实现类型转换和文件锁定机制应对高并发
android·开发语言·php
xixixi7777717 小时前
从5G标准到6G前沿:Polar码的技术演进与未来之路
开发语言·人工智能·5g·大模型·php·通信·polar码
2401_8858850418 小时前
视频短信接口集成起来复杂吗?API接入说明
开发语言·php·音视频
zopple18 小时前
PHP与Vue.js:前后端开发的完美搭档
开发语言·vue.js·php
北漂Zachary19 小时前
Laravel 8.x核心特性全解析
php·laravel
zopple19 小时前
Laravel 8.x核心特性解析
php·laravel
aq553560019 小时前
Laravel 8.x十大核心特性深度解析
php·laravel
农村小镇哥19 小时前
PHP数据传输流+上传条件+上传步骤
java·开发语言·php