访问碳对象

Accessing carbon objects

我有这个:

$now = Carbon::now();
print_r($now);

这给出了结果:

Carbon\Carbon Object
(
    [date] => 2016-09-28 06:03:16.000000
    [timezone_type] => 3
    [timezone] => UTC
)

当我尝试访问时:

print_r($now->date);

它给出错误:

Unknown getter 'date'

试试这个:

$now = Carbon::now();
$timestamp = get_object_vars($now);
print_r($timestamp);

是returns数组。

然后你可以:

echo $timestamp["date"]; 

只需使用:

echo $now;

打印日期。 Carbon 有 __toString 方法自动 returns 字符串格式的日期。