如何在不依赖于服务器日期的情况下获得时区中的适当时间?


How can I get the appropriate time in a timezone without relying on the server's date?

目前,我在PHP中使用strtotime来获取时区中特定HH:MM的时间戳。在需要进行深夜计算之前,这种方法一直运行得很好。当服务器经过午夜(UTC)时,strtotime开始返回第二天的时间戳,即使在指定的时区中仍然是同一天。我能做些什么来解决这个问题?谢谢!

下面是一些相关的代码:

/* Convert a time (HH:MM) to a localized timestamp */
static function toLocalTime($time, $timezone) {
    switch (strtoupper(trim($timezone))) {
        case 'AST':
        case 'ADT':
            $tz = 'America/Puerto_Rico';
            break;
        case 'EST':
        case 'EDT':
            $tz = 'America/New_York';
            break;
        case 'CST':
        case 'CDT':
            $tz = 'America/Chicago';
            break;
        case 'MST':
            $tz = 'America/Denver';
            break;
        case 'MDT':
            $tz = 'America/Phoenix';
            break;
        case 'PST':
        case 'PDT':
            $tz = 'America/Los_Angeles';
            break;
        case 'AKST':
            $tz = 'America/Los_Angeles';
            break;
        case 'HST':
            $tz = 'Pacific/Honolulu';
            break;
        case 'HDT':
            $tz = 'America/Adak';
            break;
        default:
            Log::error('invalid timezone: ' . $timezone . ' [' . $time . ']');
            return '';
    }
    return strtotime($time . ' ' . $tz);
}

查看strtotime()的最新在线手动条目,它似乎没有显式的timezone参数,也没有任何示例包含时区。这可能会给您带来问题。