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

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

相关推荐
成都大菠萝13 小时前
Android Car CarProperty 车辆信号链路
android
敲代码的鱼13 小时前
PDF 预览与签名批注写回 支持安卓 iOS 鸿蒙 UTS插件
android·前端·ios
时光足迹14 小时前
uni-app 视频通话实战:康复师与患者视频问诊的 6 个致命 Bug 与解决方案
android·ios·uni-app
Coffeeee19 小时前
闲聊几句,Android老哥们,你们多久没做技改需求了
android·程序员·代码规范
萝卜er19 小时前
Fragment 生命周期与状态恢复-《Android深水区(四)》
android
萝卜er19 小时前
Intent 显式、隐式与 PendingIntent-《Android深水区(五)》
android
Kapaseker1 天前
一文吃透 Kotlin 集合操作符
android·kotlin
三少爷的鞋1 天前
Main-safe:现代Android 架构真正的分水岭
android
沐怡旸1 天前
深入解析 Android Performance Analyzer (APA) 底层架构与技术原理
android
李斯维2 天前
从历史的角度看 Android 软件架构
android·架构·android jetpack