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

相关推荐
爱吃提升40 分钟前
Yifan Hu(适合大规模数据)大数据算法
开发语言·算法·php
树下水月2 小时前
php artisan serve 在window上执行报错的问题
开发语言·php
棒棒的唐2 小时前
开发中,如何指定不同的php版本启动yii项目
开发语言·php
Cyber4K13 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
Le_ee14 小时前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php
Johnstons19 小时前
Wireshark 和 tcpdump 到底怎么选?网络故障排查实战中的边界、判断标准与落地清单
wireshark·php·es·tcpdump·抓包分析·抓包与协议分析工具选型
暮雨疏桐21 小时前
阿里云从 OSS 将 CSV 推送至内网机器方案
阿里云·云计算·php
laomocoder1 天前
Project-Nexus-WAN-跨公网Agent对话
开发语言·php
d111111111d1 天前
MQTT+STM32+ESP8266网络程序分层+韦老师
笔记·stm32·单片机·嵌入式硬件·学习·php
听海边涛声1 天前
phpStudy的下载、安装
php·phpstudy