使用日期时间
<?
// 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);