PHP-将12小时格式的时间转换为新时区时出错



在将12小时格式的时间从时区转换为新时区时,我有时会收到错误时区

错误:

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

MyCodes:

// $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); 
$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;

问题:问题出在哪里
P.S:上面的代码是我的udp套接字的一部分,从php-cli-

运行

摘要

简单地说,PHP试图(出于您的需要,错误地)将AM读取为时区,而AM不是有效的时区。


详细信息

10:39:6 AM被分为以下几块

  • 10:39:6,它识别为timelong24(24小时格式化时间,带秒)
  • 它识别为tz(时区)的AM

相反,对于10:39:06 AM,它正确地将整个字符串解析为timelong12(一个12小时的格式化时间,带有秒和AM/PM)。

镗刀

这种奇怪行为的原因在于日期解析逻辑(源代码)。相关模式为:

时间长度24

't'?
[01]?[0-9] | "2"[0-4]              # hour24
[:.]
[0-5]?[0-9]                        # minute
[:.]
[0-5]?[0-9] | "60"                 # second;

时间长度12

"0"?[1-9] | "1"[0-2]               # hour12
[:.]
[0-5]?[0-9]                        # minute
[:.]
[0-5][0-9] | "60"                  # secondlz
[ t]*                             # space?
([AaPp] "."? [Mm] "."?) [00t ]  # meridian;

正如您所看到的,为了匹配timelong12格式(这正是我们真正想要的),部分的时间必须是两位数。

相关内容

  • 没有找到相关文章

最新更新