2011-03-16 16 views
1

私は、ユーザーが時間と日(または複数の日)を選択し、その日と時刻をUTC時刻。私はgmtオフセット量を各ユーザーに(ユーザーはサインアップ時に設定します)。例えば:ユーザーの日時をサーバーの日時に変換する

東部のタイムゾーンでユーザーが選択します。

午後3時15分、月曜日、火曜日、金曜日

私はその情報はUTC時間にどうなるか、時間と日を知る必要があります。解決策は、月曜日のような状況を1つのタイムゾーンで取らなければなりません。これは、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

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 

EDIT:今完璧に動作するはずささいなことを、忘れ続けました。仕事をするための機能メイド

+0

はポストをありがとうございました。唯一のことは、ユーザーが特定の日付または月、単なる日(月、火など)を一般的に入力しないことです。 – user396404