Last active
September 1, 2024 17:51
-
-
Save FragsterAt/30eb72d2b8e71a90f7eb535822dee6db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Controllers; | |
| use App\Providers\Onec; | |
| use Illuminate\Database\Eloquent\Model; | |
| use Illuminate\Http\Request; | |
| use Illuminate\Http\Resources\Json\JsonResource; | |
| use Illuminate\Support\Carbon; | |
| class TestModel extends Model | |
| { | |
| protected $guarded = []; | |
| protected $casts = [ | |
| 'd' => 'datetime', | |
| 'd_tz' => 'datetime', | |
| ]; | |
| } | |
| class TestResource extends JsonResource | |
| { | |
| /** | |
| * Transform the resource into an array. | |
| * | |
| * @return array<string, mixed> | |
| */ | |
| public function toArray(Request $request): array | |
| { | |
| return [ | |
| 'd_timezone' => $this->d->timezoneName, | |
| 'd' => $this->d->toDateTimeLocalString(), | |
| 'd_tz_timezone' => $this->d->timezoneName, | |
| 'd_tz' => $this->d_tz->toDateTimeLocalString(), | |
| ]; | |
| } | |
| } | |
| class TestController extends Controller | |
| { | |
| public function __invoke(Request $request, Onec $onec) | |
| { | |
| $d = "2024-09-01T16:00:00.000"; | |
| $d_tz = "2024-09-01T19:00:00.000+03:00"; | |
| $a = new TestModel(['d' => $d, 'd_tz' => $d_tz]); | |
| dump( | |
| config('app.timezone'), | |
| [ | |
| 'd' => $d, | |
| 'carbon' => Carbon::parse($d)->toDateTimeLocalString(), | |
| 'tz' => Carbon::parse($d)->timezoneName, | |
| 'carbon_tz UTC' => Carbon::parse($d)->timezone('UTC')->toDateTimeLocalString(), | |
| 'carbon_tz +03:00' => Carbon::parse($d)->timezone('Europe/Moscow')->toDateTimeLocalString() | |
| ], | |
| [ | |
| 'd_tz' => $d_tz, | |
| 'carbon' => Carbon::parse($d_tz)->toDateTimeLocalString(), | |
| 'tz' => Carbon::parse($d_tz)->timezoneName, | |
| 'carbon_tz UTC' => Carbon::parse($d_tz)->timezone('UTC')->toDateTimeLocalString(), | |
| 'carbon_tz +03:00' => Carbon::parse($d_tz)->timezone('Europe/Moscow')->toDateTimeLocalString() | |
| ], | |
| (new TestResource($a))->toArray($request), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment