Files
hyperf_service/app/Extend/DateUtil.php
2025-04-08 17:59:24 +08:00

191 lines
4.6 KiB
PHP

<?php
namespace App\Extend;
use DateMalformedStringException;
use DateTime;
class DateUtil
{
public const MINUTE = 60; // 分
public const HOUR = 3600; // 小时
public const DAY = 86400; // 天
public const WEEK = 604800; // 周
public const MONTH = 2592000; // 月
public const YEAR = 311040000; // 年
public const MS = 1000;
/**
* 获取当前时间到今日结束的秒数
* @return int
*/
static function getTodayEnds(): int
{
return 86400 - (time() + 8 * 3600) % 86400;
}
/**
* 获取当前时间
* @return string
*/
static function getCurrentDatetime(): string
{
return date('Y-m-d H:i:s');
}
/**
* 获取今日开始的时间
* @return string
*/
static function getTodayStartDate(): string
{
return date("Y-m-d 00:00:00");
}
/**
* 获取今日结束的时间
* @return string
*/
static function getTodayEndDate(): string
{
return date("Y-m-d 23:59:59");
}
/**
* 获取当前日期
* @return string
*/
static function getCurrentDate(): string
{
return date('Y-m-d');
}
/**
* 获取某天开始的时间
* @param int $i
* @return string
*/
static function getStartDate(int $i = 1): string
{
return date("Y-m-d 00:00:00", strtotime("-{$i} day"));
}
/**
* 获取某日结束的时间
* @param int $i
* @return string
*/
static function getEndDate(int $i = 1): string
{
return date("Y-m-d 23:59:59", strtotime("-{$i} day"));
}
/**
* 获取某天前日期
* @param int $i
* @return string
*/
static function getDate(int $i = 1): string
{
return date('Y-m-d', strtotime("-{$i} day"));
}
/**
* 获取当前的毫秒时间戳(13位)
* @return string
*/
static function getCurrentMsTime(): string
{
list($ms, $sec) = explode(' ', microtime());
$msTime = (float)sprintf('%.0f', (floatval($ms) + floatval($sec)) * 1000);
return substr($msTime,0,13);
}
/**
* 获取指定日期段内每一天的日期 (开始时间必须小于结束时间)
* @param string $startDate
* @param string $endDate
* @param string $returnFormat
* @return array
*/
static function getDateFromRange(string $startDate, string $endDate, string $returnFormat = 'Y-m-d'): array
{
if (empty($startDate) || empty($endDate)) return [];
if ($endDate < $startDate) return [];
$sTimestamp = strtotime($startDate);
$eTimestamp = strtotime($endDate);
// 计算日期段内有多少天
$days = ($eTimestamp-$sTimestamp)/86400+1;
// 保存每天日期
$date = [];
for($i=0; $i<$days; $i++){
$date[] = date($returnFormat, $sTimestamp+(86400*$i));
}
return $date;
}
static function getThisWeekInfo(): array
{
// 获取当前时间
$now = new DateTime();
// 获取开始日期 (周一)
$startOfWeek = clone $now;
$startOfWeek->modify('Monday this week');
// 获取上周的结束日期 (周日)
$endOfWeek = clone $now;
$endOfWeek->modify('Sunday this week');
// 获取上周是当年的第几周
$weekNumber = $now->format('W');
$year = $now->format('o');
return [
'start_date' => $startOfWeek->format('Y-m-d'),
'end_date' => $endOfWeek->format('Y-m-d'),
'week_number' => (int)$weekNumber,
'year' => (int)$year,
];
}
/**
* @return array
* @throws DateMalformedStringException
*/
static function getLastWeekInfo(): array
{
// 获取当前时间
$now = new DateTime();
// 获取上周的时间
$lastWeek = clone $now;
$lastWeek->modify('-1 week');
// 获取上周的开始日期 (周一)
$startOfWeek = clone $lastWeek;
$startOfWeek->modify('Monday this week');
// 获取上周的结束日期 (周日)
$endOfWeek = clone $lastWeek;
$endOfWeek->modify('Sunday this week');
// 获取上周是当年的第几周
$weekNumber = $lastWeek->format('W');
$year = $lastWeek->format('o');
return [
'start_date' => $startOfWeek->format('Y-m-d'),
'end_date' => $endOfWeek->format('Y-m-d'),
'week_number' => (int)$weekNumber,
'year' => (int)$year,
];
}
}