Php 无法从对象访问值
我正在尝试从此对象获取日期和时间:Php 无法从对象访问值,php,laravel,Php,Laravel,我正在尝试从此对象获取日期和时间: Recurr\RecurrenceCollection Object([elements:Doctrine\Common\Collections\ArrayCollection:private]=>Array([0]=>Recurr\recurrenceobject([start:protected]=>DateTime Object([date]=>2018-01-24 01:00:00.000000[timezone\u type]=>3[timezone
Recurr\RecurrenceCollection Object([elements:Doctrine\Common\Collections\ArrayCollection:private]=>Array([0]=>Recurr\recurrenceobject([start:protected]=>DateTime Object([date]=>2018-01-24 01:00:00.000000[timezone\u type]=>3[timezone]=>UTC][end:protected]=>DateTime Object([date]=>2018-01-24 01:00:00.000000])[时区类型]=>3[时区]=>UTC[索引:受保护]=>1))
我已通过以下方式访问dateTime对象:
foreach($events as $event){
$event=(array)$event;
foreach($event as $key=>$value){
echo $key.'<br>';
//echo date_format(new DateTime($value),'F d,Y');
//echo $value->format('Y-m-d H:i:s');
}
print_r($event);
}
foreach($events as$event){
$event=(数组)$event;
foreach($key=>$value形式的事件){
回显$key.“
”;
//回显日期格式(新日期时间($value),'fd,Y');
//echo$value->format('Y-m-dh:i:s');
}
打印(事件);
}
但是现在如果我尝试使用:echo$value->format('Y-m-dh:I:s');
它说:调用整数上的成员函数format()
我不知道我做错了什么。我在这里读了近100篇关于堆栈溢出的帖子,但没有任何帮助。如果有人能帮我解决这个问题,请
我需要的是:来自DateTime对象的日期和时间。这些字段是受保护的,因此不能像那样访问它们。您应该在类liek公共函数getDate(){return$this->Date}
中添加getter,现在您可以通过$Object->getDate()访问它
或将该日期属性的访问修饰符更改为public。您无需将$event
强制转换为数组
。尝试以下操作:
foreach($events as $event){
$start = $event->getStart();
echo $start->format('Y-m-d H:i:s');
}
echo(new\DateTime($value))->format('fd,Y');
@Dharman我也用过这个,它说:DateTime::\uu构造()期望参数1是字符串,对象给定的
var\u dump($value)),所以你知道它是什么。太棒了!!它工作起来像魔术一样。非常感谢你的帮助,我真的很感激:)