【问题标题】:Converting user's day and time to server's day and time in php在php中将用户的日期和时间转换为服务器的日期和时间
【发布时间】:2011-03-16 07:21:21
【问题描述】:

我有一个场景,用户选择一个时间和日期(或多天),并且该值必须转换为该日期和时间的 UTC 时间。我有每个用户的 gmt 偏移量(用户在注册时设置它)。例如:

东部时区的用户选择:

周一、周二、周五下午 3:15

我需要知道该信息在 UTC 时间中的时间和日期。解决方案必须考虑到这样的情况,例如一个时区的星期一可能是 UTC 时间的另一天。另外,如果可以将时间转换为 24 小时格式,那将是一个加分项。

为了清楚起见,应该返回类似于数组的内容,例如:

Array('<3:15 pm eastern adjusted for utc>', '<Monday adjusted for UTC>', '<Tuesday adjusted for UTC>', '<Friday adjusted for UTC>');

我不需要将结果直接格式化为这样的数组 - 这只是最终目标。

我猜这涉及到使用 strtotime,但我就是不知道该怎么做。

【问题讨论】:

    标签: php datetime time timezone


    【解决方案1】:
     $timestamp = strtotime($input_time) + 3600*$time_adjustment;
    

    结果将是一个时间戳,这是一个示例:

     $input_time = "3:15PM 14th March";
     $time_adjustment = +3;
    
     $timestamp = strtotime($input_time) + 3600*$time_adjustment;
    
     echo date("H:i:s l jS F", $timestamp);
     // 16:15:00 Monday 14th March
    

    编辑:一直忘记一些小事,现在应该可以正常工作了。

    【讨论】:

    • 感谢您的帖子。唯一的问题是用户不会输入特定的日期或月份,通常只是一天(星期一、星期二等)。
    【解决方案2】:

    制作了一个函数来完成这项工作:

    <?
    
    /*
     * The function week_times() converts a a time and a set of days into an array of week times. Week times are how many seconds into the week 
     * the given time is. The $offset arguement is the users offset from GMT time, which will serve as the approximation to their
     * offset from UTC time
     */
    // If server time is not already set for UTC, uncomment the following line
    //date_default_timezone_set('UTC');
    function week_times($hours, $minutes, $days, $offset)
    {
    
        $timeUTC = time(); // Retrieve server time
    
        $hours += $offset; // Add offset to user time to make it UTC time
    
        if($hours > 24) // Time is more than than 24 hours. Increment all days by 1
        {
    
            $dayOffset = 1;
            $hours -= 24; // Find out what the equivelant time would be for the next day
    
        }
        else if($hours < 0) // Time is less than 0 hours. Decrement all days by 1
        {
    
            $dayOffset = -1;
            $hours += 24; // Find out what the equivelant time would be for the prior day
    
        }
    
        $return = Array(); // Times to return
    
        foreach($days as $k => $v) // Iterate through each day and find out the week time
        {
    
            $days[$k] += $dayOffset;
    
            // Ensure that day has a value from 0 - 6 (0 = Sunday, 1 = Monday, .... 6 = Saturday)
            if($days[$k] > 6) { $days[$k] = 0; } else if($days[$k] < 0) { $days[$k] = 6; }
    
            $days[$k] *= 1440; // Find out how many minutes into the week this day is
            $days[$k] += ($hours*60) + $minutes; // Find out how many minutes into the day this time is
    
        }
    
    
        return $days;
    
    }
    
    ?>
    

    【讨论】:

    • 这也是我需要的(对于 iCal 的 BYDAY 功能,用户在他们的时区输入“星期一”,但我们正在转换为 UTC,它可能是偏移后的“星期日/星期二” ): 这是我可以从这里开始的好地方:)
    猜你喜欢
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多