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

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

相关推荐
不是万能的豆学长2 小时前
filetidy 文件整理助手:一键归类 + 规范重命名(Python 零依赖单文件工具)
大数据·开发语言·python·技术美术
小白跃升坊4 小时前
PHP2Shell:AI 两天挖出 9.2 分漏洞,让全球 70% 网站连夜打补丁!1Panel 已经把修复版本封装好,推到了你的应用商店里。
php·开源安全·1panel·ai安全·漏洞披露
懂点Tech6 小时前
ctfshow MISC入门图片篇(信息附加):misc41解题思路
开发语言·python
Json____7 小时前
PHP 突发严重安全漏洞,AI 开始改变网络安全的攻防方式
人工智能·web安全·php·it学习·wwwoop.com
catchadmin7 小时前
PHP 8.7 协程新提案 让跨协程代码共享请求身份
开发语言·php
念何架构之路7 小时前
go-grpc设计思想
开发语言·后端·golang
Web极客码8 小时前
我用 Claude 分析 Redis 哨兵日志,实现跨机房数据不一致自动对齐
数据库·redis·php
吹什么轩8 小时前
MySQL:数据类型
android·adb
酷炫码神8 小时前
Day 10|Pinia 状态管理:把散落在各组件的数据,收进一个全局仓库
android·开发语言·javascript
xuefeiniao9 小时前
PHP2Shell漏洞修复
运维·服务器·php