我有一个场景,用户选择时间和日期(或多天),并且该值必须转换为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,但我不能完全指责如何去做。
答案 0 :(得分: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
编辑:一直忘记一些小事情,现在应该做得很好。
答案 1 :(得分: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;
}
?>