perf: 优化时间组件

This commit is contained in:
kuaifan
2024-09-21 19:48:29 +03:00
parent a7bd0e0dac
commit a93345afbd
19 changed files with 155 additions and 96 deletions

View File

@@ -6,6 +6,7 @@ use App\Exceptions\ApiException;
use App\Models\Setting;
use App\Models\Tmp;
use Cache;
use Carbon\Carbon;
use Overtrue\Pinyin\Pinyin;
use Redirect;
use Request;
@@ -1661,7 +1662,7 @@ class Base
public static function forumDate($date)
{
$dur = time() - $date;
if ($date > strtotime(date("Y-m-d"))) {
if ($date > Carbon::now()->startOf('day')->timestamp) {
//今天
if ($dur < 60) {
return max($dur, 1) . '秒前';
@@ -1672,10 +1673,10 @@ class Base
} else {
return date("H:i", $date);
}
} elseif ($date > strtotime(date("Y-m-d", strtotime("-1 day")))) {
} elseif ($date > Carbon::now()->subDays()->startOf('day')->timestamp) {
//昨天
return '昨天';
} elseif ($date > strtotime(date("Y-m-d", strtotime("-2 day")))) {
} elseif ($date > Carbon::now()->subDays(2)->startOf('day')->timestamp) {
//前天
return '前天';
} elseif ($dur > 86400) {
@@ -1686,23 +1687,22 @@ class Base
}
/**
* 获取时间戳今天的第一秒时间戳
* @param $time
* @return false|int
* 创建Carbon对象
* @param $var
* @return Carbon
*/
public static function dayTimeF($time)
public static function newCarbon($var)
{
return strtotime(date("Y-m-d 00:00:00", self::isNumber($time) ? $time : strtotime($time)));
}
/**
* 获取时间戳今天的最后一秒时间戳
* @param $time
* @return false|int
*/
public static function dayTimeE($time)
{
return strtotime(date("Y-m-d 23:59:59", self::isNumber($time) ? $time : strtotime($time)));
if (self::isNumber($var)) {
if (preg_match("/^\d{13,}$/", $var)) {
$var = $var / 1000;
}
return Carbon::createFromTimestamp($var);
} elseif (is_string($var)) {
return Carbon::parse(trim($var));
} else {
return Carbon::now();
}
}
/**
@@ -2631,18 +2631,6 @@ class Base
return array_values($arr[$key]);
}
/**
* 获取当前是本月第几个星期
* @return float
*/
public static function getMonthWeek()
{
$time = strtotime(date("Y-m-01"));
$w = date('w', $time);
$j = date("j");
return ceil(($j . $w) / 7);
}
/**
* 把返回的数据集转换成Tree
* @param array $list 要转换的数据集

View File

@@ -31,12 +31,8 @@ class TimeRange
$range = $this->format($data);
}
//
$updated = Base::isNumber($range[0]) ? intval($range[0]) : trim($range[0]);
$deleted = Base::isNumber($range[1]) ? intval($range[1]) : trim($range[1]);
//
$timezone = config('app.timezone');
$this->updated = $updated ? Carbon::parse($updated)->setTimezone($timezone) : null;
$this->deleted = $deleted ? Carbon::parse($deleted)->setTimezone($timezone) : null;
$this->updated = $range[0] ? Base::newCarbon($range[0]) : null;
$this->deleted = $range[1] ? Base::newCarbon($range[1]) : null;
}
/**
@@ -72,7 +68,7 @@ class TimeRange
private function format($timerange)
{
$search = str_contains($timerange, ":") ? ["|"] : ["|", "-"];
return explode(",", str_replace($search, ",", $timerange));
return Base::newTrim(explode(",", str_replace($search, ",", $timerange)));
}
/**