1

当我回声时Carbon::now()。它仅显示日期时间。“2018-07-05 09:21:21”

但是当我在 MongoDB 上插入它时,它变成了一个对象;

{
    "date" : "2018-07-05 09:21:21.020981",
    "timezone_type" : 3,
    "timezone" : "UTC" 
}

我只想要date. 为什么会这样?

编辑:

我试着这样做

$currentDateTime = strtotime(Carbon::now());

$request->merge([
    'dateSample' => date("Y-m-d H:i:s", $currentDateTime)
]);

但它保存为string类型。我需要它是Date类型。

4

2 回答 2

5

因为当你echo Carbon::now()那个时候调用__toString函数时,这里是一个代码。

public function __toString()
{
    return $this->format(static::$toStringFormat);
} 

如果你使用dd(Carbon::now());你必须看到这个结果

{
    "date" : "2018-07-05 09:21:21.020981",
    "timezone_type" : 3,
    "timezone" : "UTC" 
}

echo Carbon::now();这种情况下你看到结果Carbon::now()->__toString()是“2018-07-05 09:21:21”时;
编辑
对于获取DateTime对象,您可以

$currentDateTime = \Carbon\Carbon::now()->toDateTimeString();
$date = new DateTime($currentDateTime);
于 2018-07-05T09:29:54.447 回答
0

确保您的数据库表列设置为 date example_datedate DEFAULT NULL 类型,

哎呀,我的错误你想使用 merge()

$request->merge([
    'dateSample' => \Carbon\Carbon::now()->format('Y-m-d');
]);
于 2018-07-05T10:15:17.600 回答