要获取 PHP 中当前时间的前一天、本周、本月、本季度和本年,可以使用 PHP 的内置日期和时间函数。

要获取 PHP 中当前时间的前一天、本周、本月、本季度和本年,可以使用 PHP 的内置日期和时间函数。下面是一些示例代码来帮助你实现这些功能:

php 复制代码
php
// 获取当前时间的前一天
$yesterday = date('Y-m-d', strtotime('-1 day'));

// 获取本周的开始日期和结束日期
$thisWeekStart = date('Y-m-d', strtotime('this week'));
$thisWeekEnd = date('Y-m-d', strtotime('this week +6 days'));

// 获取本月的开始日期和结束日期
$thisMonthStart = date('Y-m-01');
$thisMonthEnd = date('Y-m-t');

// 获取本季度的开始日期和结束日期
$currentQuarter = ceil(date('n') / 3);
$thisQuarterStart = date('Y-m-d', strtotime('first day of january +' . ($currentQuarter - 1) * 3 . ' months'));
$thisQuarterEnd = date('Y-m-d', strtotime($thisQuarterStart . ' +2 months last day of'));

// 获取本年的开始日期和结束日期
$thisYearStart = date('Y-01-01');
$thisYearEnd = date('Y-12-31');

上述代码中,我们使用 date() 函数按照指定格式获取日期,并使用 strtotime() 函数处理相对于当前时间的日期偏移。这些函数结合使用可以轻松获取所需的日期范围。

请注意,以上代码假设你的服务器时区设置正确。如果你的服务器时区不正确,可能需要在代码中添加适当的时区设置。

相关推荐
眠りたいです3 分钟前
现代C++:C++17中的新库特性
开发语言·c++·c++20·c++17
杉氧3 分钟前
100% Kotlin:基于 KMP + Compose Multiplatform 的全栈架构实战(Clean Architecture + MVI)
android·架构
devnullcoffee7 分钟前
亚马逊 Buy Box 数据采集完全指南(2026):Python 实战 + Pangolinfo API
开发语言·python·亚马逊数据采集·亚马逊数据 api·pangolinfo api·亚马逊 buy box 数据·亚马逊数据采集软件
sleven fung9 分钟前
Whisper库
开发语言·人工智能·python·算法·ai·whisper
小仙女喂得猪16 分钟前
AI 写 Android 代码老翻车?我把移动端的 Harness 系统开源了
android·github·ai编程
天若有情67327 分钟前
【C++趣味实战】仿写Burp代理逻辑!自定义可控迭代器:拦截Intercept/放行Forward/重放Repeater全实现
java·开发语言·c++
l1t28 分钟前
DeepSeek总结的使用实体-组件-系统和基于存在性处理进行Python编程37-38
开发语言·python
迷藏49428 分钟前
Python+DuckDB:轻量级BI流水线实战
java·开发语言·python·原型模式
杉氧30 分钟前
第一篇:从一个 Dagger 报错开始:手把手带你搭建 Hilt 依赖注入的护城河
android·架构
咋吃都不胖lyh32 分钟前
短期记忆和长期记忆都存 MySQL
android·java·开发语言