要获取 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() 函数处理相对于当前时间的日期偏移。这些函数结合使用可以轻松获取所需的日期范围。

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

相关推荐
lsx2024069 分钟前
Razor VB 循环
开发语言
csuzhucong10 分钟前
c++版本特性
开发语言·c++
私人珍藏库29 分钟前
【Android】小小最新AI--千变万化扮演任何角色--沉浸式互动
android·app·工具·软件·多功能
wand codemonkey32 分钟前
【第五步+前后分离调】最后的联动调试--java+Vue3项目
java·开发语言·vue.js
RSTJ_162536 分钟前
PYTHON+AI LLM DAY FOURTY-SEVEN
开发语言·人工智能·python·深度学习
代钦塔拉1 小时前
第二篇:VS2019 + Qt5.9.9 中文乱码实战:源码GB2312不乱码、文件写入与跨平台方案
开发语言·qt
zh_xuan1 小时前
Android MVI架构
android·mvi
.小小陈.1 小时前
Linux 多线程进阶:线程互斥、同步、线程池、死锁与线程安全、读写锁、自旋锁
linux·开发语言·c++
lingran__1 小时前
C++入门基础
开发语言·c++
测试开发-学习笔记2 小时前
Airtest+Poco快速上手
android·其他