1

以下のコードを使用して、12時間UTCを新しいタイムゾーン
MySourceCodesに変換します:(私のudpソケットサーバーコードの一部)

// $gps_time = "9:43:52"; 
$gps_time      = $time_hour.":".$time_min.":".$time_sec;
// $time_received = "01:45:04 2012-07-28"; 
$time_received = date('H:i:s Y-m-d');

$utc = new DateTimeZone("UTC"); 
$moscow = new DateTimeZone("Europe/Moscow"); 

//Instantiate both AM and PM versions of your time 
$gps_time_am = new DateTime("$gps_time AM", $utc); 
$gps_time_pm = new DateTime("$gps_time PM", $utc); 

//Received time 
$time_received = new DateTime($time_received, $moscow); 

//Change timezone to Moscow 
$gps_time_am->setTimezone($moscow); /* ### ### Line 105 Error ### ### */
$gps_time_pm->setTimezone($moscow); 

//Check the difference in hours. If it's less than 1 hour difference, it's the correct one. 
if ($time_received->diff($gps_time_pm)->h < 1) { 

$correct_time = $gps_time_pm->format("H:i:s Y-m-d");
} 
else { 

$correct_time = $gps_time_am->format("H:i:s Y-m-d");
}

echo $correct_time;

PHPエラー:

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (0:11:38 AM) at position 8 (A): The timezone could not be found in the database' in C:\xampp\php\udp.php:105
Stack trace:
#0 C:\xampp\php\udp.php(105): DateTime->__construct('0:11:38 AM', Object(DateTimeZone))
#1 {main}  thrown in C:\xampp\php\udp.php on line 105

入力時間が0:11:38 AM
質問の場合にエラーが発生します:
どうすれば問題を解決できますか?

編集: GPSトラッカーからの入力時間ソース(秒を12時間のフォーマット時間に変更)

$time_hour      = floor($get_time / 3600);
$time_min       = floor(($get_time - ($time_hour*3600)) / 60);
$time_sec       = dotwodigits(($get_time - (($time_hour*3600) + ($time_min * 60))));
GPS Time        = $time_hour.":".$time_min.":".$time_sec
4

1 に答える 1

0

コメントで指摘したように、12時間の時間には時間がありません0:xx:yy。次に、できることは、$time_hour事前にの値を確認することです。

//assigning values to $time_hour, $time_min, and $time_sec
if ($time_hour == 0) $time_hour = 12;
//rest of code

DateTimeこれにより、無効な時間がコンストラクターに渡されるのを防ぐことができます。

編集:このチェックは、実際には$time_hour:の割り当て中に実行できます。

$time_hour = floor($get_time / 3600) ?: 12;
于 2012-07-30T14:57:10.767 に答える