gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:37:51 24 4
gpt4 key购买 nike

我收到错误 有时 将 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:\xampp\php\udp.php:105
Stack trace:
#0 C:\xampp\php\udp.php(105): DateTime->__construct('00:00:31 AM', Object(DateTimeZone))
#1 {main} thrown in C:\xampp\php\udp.php on line 105

我的代码:
// $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 小时格式时间)
  • AM它识别为 tz (时区)。

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

    无聊的钻头

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

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

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

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

    关于PHP - 将 12 小时格式时间转换为新时区时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11701757/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com