I am trying to find a workaround for how I can convert the datetime stored in my Database as 'yyyy-mm-dd HH-mm-ss' and give it the timezone 'America/Los_Angeles'.

If I change the timezone, Carbon will automatically subtract 7 hours from the time, which is what happens when changing the time from UTC to PST, but the time in my DB is set for PST time. For example, I want the time to be 10am today, but it I change the timezone, Carbon will convert it to 3am today.

How can I make it where the timezone gets changed to PST but still keep the time at 10am? I need the timezone for an API call, which is why I need this figured out.

推荐答案

Ok, I figured out a dumb, but functional way of doing what I wanted to do, and it works.

这是我的代码:

      $dt = Carbon::parse($request->ShootDateTime)->timezone('America/Los_Angeles');
      $toDay = $dt->format('d');
      $toMonth = $dt->format('m');
      $toYear = $dt->format('Y');
      $dateUTC = Carbon::createFromDate($toYear, $toMonth, $toDay, 'UTC');
      $datePST = Carbon::createFromDate($toYear, $toMonth, $toDay, 'America/Los_Angeles');
      $difference = $dateUTC->diffInHours($datePST);
      $date = $dt->addHours($difference);

I get the time I wanted to convert to a timezone and parse it and change the timezone. I then get the Day, Month, and Year and create 2 different Dates, both will read as being 00:00:00 in time, but because they are separate by 7 hours in this instance, they can only both be 00:00:00 if they are 7 hours different, which is what I capture next. Then finally, I take the initial datetime and add the difference to it, giving me the proper time in the proper timezone.

It's ugly, but it works.

Laravel相关问答推荐

如何重置laravel ui密码

Laravel FAKER语法

使用 Laravel 10 在 Blade 中添加成功消息

Inertiajs - 使用链接组件添加记录但无法在不使用表单的情况下清除文本区域

如何使用 laravel 更新单个值

Laravel Horizo​​n 限制与优化

Laravel:BelongstoMany 关系实现

Laravel 5 - 更改模型文件位置

如何更新 Laravel 4 中现有的 Eloquent 关系?

如果用户在 Laravel 中经过身份验证,如何签入 Vue 组件?

artisan 迁移错误找不到类'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver'

Laravel 将参数从路由传递到过滤器

Laravel 5 干预中的图像验证

Laravel Eloquent模型如何从关系表中获取数据

使用哪个 Auth::check() 或 Auth::user() - Laravel 5.1?

向 Laravel 重定向添加一个锚点

Laravel - 使用 Eloquent 查询构建器在 Select 中添加自定义列

如何在没有日志(log)、没有信息的情况下调试 Laravel 错误 500

Laravel如何仅响应没有正文消息的204代码状态

Laravel 5 Auth注销不起作用