如何根据用户时区转换时间,而没有日期PHP Laravel

发布于 2025-02-13 06:46:44 字数 799 浏览 1 评论 0原文

我在数据库中有一个名为usershifts的表,它有两个time键入列以及其他列,第一个是start_time,第二个是<代码> end_time ,并且都具有时间datatype。我不允许将这些列类型更改为DateTime或Timestamp。而且也不允许在表中创建另一列。现在,我的任务是显示已存储在start_timeend_time列中的时间timezone exemper utc TimeZone用户应该能够按照UTC时区和其他时区用户的时间来计时,也应该能够根据他的时区观看。 现在问题是我应该如何在数据库中存储时间以及我应该存储时间的时区。最后的问题是,我应该如何根据不同的时区显示时间。 我尝试关注代码,但您可以看到我必须处理日期才能做到这一点。我不想约会。

 $s_time=date('Y-m-d '.$shift->start_time.'');
 $start_time = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $s_time, 'UTC');
 $e_time=date('Y-m-d '.$shift->end_time.'');


 $end_time = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $e_time, 'UTC');

I have a table in database named USERSHIFTS and it has two TIME type columns along with other columns first one is it start_time and second one is end_time and both have TIME datatype . i am not allowed to change these columns types to datetime or timestamp. And also not allowed to create another column in table. Now my task is to display time that has been stored in start_time and end_time column according to user timezone for example UTC timezone user should be able to time according to UTC timezone and Other timezone user should also be able to see this time according to his timezone .
Now question is how should i store time in database and in which timezone i should store time. And Final question is how should i display time according to different timezones.
i tried following code but you can see i have to deal with date in order to do this. And i dont want date .

 $s_time=date('Y-m-d '.$shift->start_time.'');
 $start_time = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $s_time, 'UTC');
 $e_time=date('Y-m-d '.$shift->end_time.'');


 $end_time = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $e_time, 'UTC');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红颜悴 2025-02-20 06:46:44

此直接将日期从数据库转换为您想要的内容。在此功能中,您将时间转换为您想要的时区时间

$start_time = \Carbon\Carbon::createFromFormat('H:i:s', $shift->start_time, 'UTC')->format('H:i:s');
$end_time = \Carbon\Carbon::createFromFormat('H:i:s', $shift->end_time, 'UTC')->format('H:i:s');

This convert directly the date from the database to this what you want. In this function you convert time to the timezone time you want

$start_time = \Carbon\Carbon::createFromFormat('H:i:s', $shift->start_time, 'UTC')->format('H:i:s');
$end_time = \Carbon\Carbon::createFromFormat('H:i:s', $shift->end_time, 'UTC')->format('H:i:s');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文