日期时间转换:“在数据库中找不到时区”


DateTime conversion: "The timezone could not be found in the database"

我正在尝试转换从API接收的日期时间。

他们是这样生成datetime字符串的:

public static function formatDate($date) {
    $format = "Y-m-d'TH:i:sP";
    if ($date instanceof DateTime) {
        $d = $date->format($format);
    } elseif (is_numeric($date)) {
        $d = date($format, $date);
    } else {
        $d = (String) "$date";
    }
    return $d;
}

给我

 "2013-06-14T04:00:36.000-03:00"

我要做的是把它转换回来:

try 
{ 
   $date = Carbon'Carbon::createFromFormat("Y-m-d'TH:i:sP", "2013-06-14T04:00:36-03:00");
   echo "<pre>".var_dump($date)."</pre>";
   $date = DateTime::createFromFormat("Y-m-d'TH:i:sP", "2013-06-14T04:00:36.000-03:00");
   echo "<pre>".var_dump($date)."</pre>";
   $date = Carbon'Carbon::createFromFormat("Y-m-d'TH:i:sP", "2013-06-14T04:00:36.000-03:00"); /// raise exception
   echo "<pre>".var_dump($date)."</pre>";
} 
catch(InvalidArgumentException $x) 
{ 
    echo $x->getMessage(); 
}

DateTime类给了我一个空,没有错误,但碳(https://github.com/briannesbitt/Carbon)从DateTime扩展告诉我发生了什么,这是输出:

object(Carbon'Carbon)#181 (3) { ["date"]=> string(19) "2013-06-14 04:00:36" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "-03:00" }
bool(false)
The timezone could not be found in the database

所以,去掉"。但是,如果它是由PHP创建的,使用与我用来转换它的格式完全相同的格式,为什么呢?

格式不同。接收到的字符串的正确格式是:

$format = "Y-m-d'TH:i:s.uP";