开发者

Converting user's day and time to server's day and time in php

I have a scenario in which the user selects a time and day (or multiple days) and that value must be converted to whatever that day and time would开发者_Go百科 be in UTC time. I have the gmt offset amount for each user (the users set it when they signup). For instance:

A user in the eastern timezone selects:

3:15 pm, Monday, Tuesday, Friday

I need to know what time and days that information would be in UTC time. The solution has to take into situations such Monday in one timezone can be a different day in UTC time. Also, if the time can be converted to 24 hour format, that would be a plus.

For the sake of clarity, something along the lines of an array should be returned such as:

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

I don't need the result to be directly formatted into an array like that - that's just the end goal.

I am guessing it involves using strtotime, but I just can't quite my finger out how to go about it.


 $timestamp = strtotime($input_time) + 3600*$time_adjustment;

The result will be a timestamp, here's an example:

 $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

EDIT: kept forgetting little things, that should be working perfectly now.


Made a function to do the job:

<?

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜