不使用日期时如何在Carbon中获取时区偏移?

时间:2019-05-23 20:01:44

标签: php php-carbon timezone-offset

我正在创建一个应用程序,允许人们每天保留一个小时的时间。

它实际上不是日历,因为它仅显示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');
});

屏幕截图

table

0 个答案:

没有答案