37 lines
1,011 B
PHP
37 lines
1,011 B
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Traits\Requests;
|
||
|
|
|
||
|
|
use Illuminate\Support\Facades\Date;
|
||
|
|
|
||
|
|
trait ConvertsClientDatesToUtc
|
||
|
|
{
|
||
|
|
protected function convertLocalDatetimeToUtc(string $value, string $timezone, string $format = 'Y-m-d\TH:i'): string
|
||
|
|
{
|
||
|
|
$parsed = Date::createFromFormat($format, $value, $timezone);
|
||
|
|
|
||
|
|
if (! $parsed) {
|
||
|
|
throw new \UnexpectedValueException('Invalid local datetime value');
|
||
|
|
}
|
||
|
|
|
||
|
|
return $parsed->setTimezone('UTC')->format('Y-m-d H:i:s');
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function mergeLocalDatetimeAsUtc(string $field, string $timezoneField = 'client_timezone', string $format = 'Y-m-d\TH:i'): void
|
||
|
|
{
|
||
|
|
if (! $this->filled($field)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->merge([
|
||
|
|
$field => $this->convertLocalDatetimeToUtc(
|
||
|
|
value: $this->string($field)->value(),
|
||
|
|
timezone: $this->string($timezoneField)->value(),
|
||
|
|
format: $format,
|
||
|
|
),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|