2011-03-16 87 views
1

我有一个场景,用户选择时间和日期(或多天),并且该值必须转换为当天和UTC时间。我有每个用户的gmt抵消金额(用户在注册时设置)。例如:用户的日期和时间转换为服务器的日期和时间在php

在东部时区的用户选择:

下午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,但我只是不能完全指出如何去做。

回答

1

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

<? 

/* 
* 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; 

} 

?> 
+0

这也是我需要的东西(对于iCal的BYDAY功能,用户在他们的时区输入“星期一”,但我们转换为UTC,它可能是偏移后的“星期日/星期二”):这是个好地方我可以从:)开始 – Renee 2011-05-25 20:25:51

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 

编辑:总是忘记小事,应该可以完美工作了。

+0

谢谢你的职位。唯一的一点是,用户不会输入特定的日期或月份,仅仅是一天(周一,周二等)。 – user396404 2011-03-16 19:08:01