You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.0 KiB

5 days ago
<?php
namespace App\Support;
use Carbon\Carbon;
use DateTimeInterface;
class CalendarDateFormat
{
/**
* 活动类 datetime 按应用时区输出日历日,避免 toIso8601String 在 H5/JS 中差一天。
*/
public static function ymdFromDatetime(mixed $value): ?string
{
if ($value === null || $value === '') {
return null;
}
if (! $value instanceof Carbon) {
$value = Carbon::parse($value);
}
return $value->copy()->timezone((string) config('app.timezone'))->format('Y-m-d');
}
public static function ymdFromDateValue(mixed $value): ?string
{
if ($value === null || $value === '') {
return null;
}
if ($value instanceof DateTimeInterface) {
return Carbon::instance($value)->format('Y-m-d');
}
if (is_string($value) && preg_match('/^(\d{4}-\d{2}-\d{2})/', $value, $m)) {
return $m[1];
}
return Carbon::parse($value, (string) config('app.timezone'))->format('Y-m-d');
}
}