45 lines
1.5 KiB
PHP
45 lines
1.5 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\Services;
|
||
|
||
use Illuminate\Support\Carbon;
|
||
|
||
/**
|
||
* 种菜报销统一用平台时区(config('app.platform_timezone') = APP_TIMEZONE,北京时间)取「现在」和解析时间。
|
||
*
|
||
* 不能直接用 now():页面请求里 app.timezone 会被 App\Utilities\Overrider 按公司设置覆盖,
|
||
* 没设置时回落成 Europe/London;而定时任务(镜像同步、11:20 推送)按平台时区写时间。
|
||
* 两边混用会把刚同步完的工单误判成「超过 20 分钟没同步」、默认报销日期跨天出错。
|
||
*/
|
||
final class PlantingClock
|
||
{
|
||
public static function timezone(): string
|
||
{
|
||
$timezone = trim((string) config('app.platform_timezone', ''));
|
||
|
||
return $timezone !== '' ? $timezone : 'Asia/Shanghai';
|
||
}
|
||
|
||
public static function now(): Carbon
|
||
{
|
||
return Carbon::now(self::timezone());
|
||
}
|
||
|
||
/** 解析不带时区的「Y-m-d H:i:s」时间(镜像状态、批次记录都是按平台时区写的)。 */
|
||
public static function parse(string $time): Carbon
|
||
{
|
||
return Carbon::parse($time, self::timezone());
|
||
}
|
||
|
||
public static function stamp(): string
|
||
{
|
||
return self::now()->format('Y-m-d H:i:s');
|
||
}
|
||
|
||
/** 距现在超过多少分钟(不分前后)。 */
|
||
public static function minutesSince(string $time, ?Carbon $now = null): float
|
||
{
|
||
return ($now ?? self::now())->diffInMinutes(self::parse($time), true);
|
||
}
|
||
}
|