php - 不使用日期时如何在 Carbon 中获取时区偏移量?
问题描述
我正在创建一个应用程序,它允许人们每天保留一个小时的时间。
它不是真正的日历,因为它只显示 7 天(周一 - 周日)和一天中的几个小时(0-23)。请参阅屏幕截图以供参考。
任何人,任何地方都可以注册。因此,迈阿密的人可以预订12:00 - 13:00
,Sunday
并将作为 UTC 存储在数据库中。当纽约的某个人去查看这个“时间表”时,他们会看到有人在周日下午 12:00 预订了一个小时。
但是,如果洛杉矶的某个人正在查看时间表,他们还会看到该时间已在周日预订,但需要针对他们的时区进行偏移,因此他们会看到上午 9:00 已被预订。这是按预期工作的。
现在问题来了:如果曼谷的某个人在周日预留了一个小时,我如何获得 14 小时的偏移量,以便洛杉矶的某个人将其视为周一而不是周日,并提供正确的时间?
我一直在为此绞尽脑汁,但无法提出解决方案,因为我们处于固定天数并且没有使用日期,只是几天和几小时。
功能
function daysOfTheWeek() {
return ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
}
function daysInTheWeek() {
$days = [];
for($i=1; $i<=7; $i++) {
$days[$i] = $i;
}
return $days;
}
function hoursinTheDay() {
$hours = [];
for($i=0; $i<=23; $i++) {
$hours[$i] = $i;
}
return $hours;
}
编码
use Carbon\Carbon;
$hoursReserved = \DB::table('schedule')->get();
echo '<table border="1" cellpadding="2">';
echo '<tr>';
echo '<th>Hour</th>';
// print the days of the week (Monday - Sunday)
foreach(daysOfTheWeek() as $daw) {
echo '<th>'.$daw.'</th>';
}
echo '</tr>';
foreach(hoursinTheDay() as $hour) {
echo '<tr>';
echo '<td>';
// print the hours of the day (24 hour format)
echo Carbon::createFromFormat('H',$hour)->format('H:i').' - '.Carbon::createFromFormat('H',$hour)->addHour(1)->format('H:i');
echo '</td>';
// loop through the number of days in the week (1-7)
foreach(daysInTheWeek() as $day) {
// loop through the reserved hours
foreach($hoursReserved as $reservedHour) {
// if the hour reserved is in Bangkok, and the person is viewing it in Los Angeles, how to show that offset?
// stub in the timezones for testing
// $start = Carbon::createFromFormat('H:i:s', $reservedHour->start_at)->timezone('Asia/Bangkok')->format('H:i:s');
// $now = Carbon::createFromFormat('H:i:s', $reservedHour->start_at)->timezone('America/Los_Angeles')->format('H:i:s');
// dd($now);
if(($reservedHour->day == $day) && ($reservedHour->start_at == $hour)) {
// hour is reserved - this is working for local time
// I stripped out the conversion for NYC to LA for brevity
echo '<td>X</td>';
} else {
// hour is not reserved
echo '<td></td>';
}
}
}
echo '</tr>';
}
echo '</table>';
dd();
Route::get('/', function () {
return view('welcome');
});
截屏
解决方案
推荐阅读
- json - 使用 Nominatim 和 php file_get_contents() 或 Curl 未从 openstreet 地图获取任何 json 结果
- php - WP_Query 返回未定义
- android-studio - 设置 Google Maven 依赖项以供离线使用
- c++ - cmake生成的标头导致双重构建
- python - Pycharm运行错误的版本
- javascript - dojo/modal 类型的 Dojo 关闭和退出事件
- python - 为什么kivy在运行时会显示此错误?
- xamarin.forms - 在 iOS 中使用 Xamarin.Essentials FilePicker,如何指定具有几个奇怪扩展名的 FileTypes?
- javascript - 如何使css动画响应?
- javascript - 尝试获取时出现网络错误