gpt4 book ai didi

php - 日期时间转换 : "The timezone could not be found in the database"

转载 作者:可可西里 更新时间:2023-11-01 00:09:36 26 4
gpt4 key购买 nike

我正在尝试转换从 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 类只给我一个 null,没有错误,但是从 DateTime 扩展的 Carbon (https://github.com/briannesbitt/Carbon) 告诉我发生了什么,这是输出:

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

因此,只需从有效的日期时间中删除“.000”,但为什么,如果它是由 PHP 使用我用来将其转换回来的完全相同的格式创建的呢?

最佳答案

格式不一样。您收到的字符串的正确格式是:

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

关于php - 日期时间转换 : "The timezone could not be found in the database",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17125092/

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