127 lines
2.9 KiB
PHP
127 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Extend;
|
|
|
|
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; // 年
|
|
|
|
/**
|
|
* 获取当前时间到今日结束的秒数
|
|
* @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;
|
|
}
|
|
} |