1

時間とタイムゾーンに関連する特定の機能を必要とするアプリに取り組んでいます。これは、私の疑問について簡単に説明する可能性のある以下の疑似コードです

$start_time = "10:00 AM"; // starting time of an event
$end_time = "11:00 AM"; // ending time of an event
$system_time = "1:30 AM";

$timezone = "India +5:30";

私が欲しいのは、イベントが現在進行中か、これから起こるか、またはすでに起こっているかに基づいて、イベントのステータスを「Live」、「Upcoming」、および「Finished」として表示することです。

国のタイムゾーンに関係なく、このステータスを確認するにはどうすればよいですか?

次のような普遍的な機能を持つことができる方法

$universal_time = get_time_in_utc_for($system_time, $timezone); // similarly for start_time & end_time
4

2 に答える 2

1

日時の使用

<?
// Not sure where you're gonna run this
$b = PHP_EOL . "<br/>";

// Create your events in their natual start/end time per that country/TZ
$eventStartMyTime = '2013-03-28 11:30:00';
$eventEndMyTime = '2013-03-28 16:30:00';
$eventTimeZone = new DateTimeZone('Asia/Calcutta');
$startTime = new DateTime($eventStartMyTime, $eventTimeZone);
$endTime = new DateTime($eventEndMyTime, $eventTimeZone);

// Now when the system deals with dates, it's going to
// deal with them all in UTC (DateTime object can do this)
// A Unix timestamp is inherently "in UTC"
// Store these values in a db if you need to
$startTS = $startTime->getTimestamp();
$endTS = $endTime->getTimestamp();

// Function to get status
function getStatus($dateTime, $start, $end) {

    // Get UTC timestamp for the input
    $time = $dateTime->getTimestamp();

    // Check against event times
    switch(true) {
        case $time >= $start && $time < $end: return "LIVE";
        case $time >= $end: return "ENDED";
        case $time <= $start: return "UPCOMING";
    }
}

テスト

// Let's walk through some scenarios
$testUpcoming = new DateTime('2013-03-21 00:00:00', new DateTimeZone('Asia/Calcutta'));
$testLive = new DateTime('2013-03-28 15:30:00', new DateTimeZone('Asia/Calcutta'));
$testEnded = new DateTime('2013-03-28 23:30:00', new DateTimeZone('Asia/Calcutta'));
$testNewYork = new DateTime('2013-03-28 12:30:00', new DateTimeZone('America/New_York'));
$testPyongyang = new DateTime('2013-03-28 12:30:00', new DateTimeZone('Asia/Pyongyang'));

// Use a fixed TZ (the TZ of the server)
// Should be UPCOMING
echo getStatus($testUpcoming, $startTS, $endTS) . $b;
// Should be LIVE
echo getStatus($testLive, $startTS, $endTS) . $b;
// Should be ENDED
echo getStatus($testEnded, $startTS, $endTS) . $b;

// Pretend we're running on a server in New York
// Same timestamp, different TZ
// Should be ENDED
echo getStatus($testNewYork, $startTS, $endTS) . $b;

// Pretend we're running on a server in Pyongyang
// Same timestamp, different TZ
// Should be UPCOMING
echo getStatus($testPyongyang, $startTS, $endTS) . $b;


$now = new DateTime();
echo "Our current timezone is: " . $now->getTimezone()->getName()  . $b;
echo "And the event is: " . getStatus($now, $startTime, $endTime);
于 2013-03-28T21:52:44.620 に答える
1

あなたは良いスタートを切っています - UTC を使用してください。

伝統的strtotime()に仕事のための方法です。ただし、PHP > 5.3 を使用している場合は、DateTimeクラスの方が柔軟であることに気付くかもしれません。

簡単なコード サンプル:

$timestamp = strtotime('2013-03-28 10:00 AM +5:30');
// 1364445000
于 2013-03-28T20:59:36.913 に答える