【发布时间】:2015-10-29 12:19:24
【问题描述】:
我在课堂上遇到了日期时间属性问题。
下一个代码在我的树枝模板中。
<div class="process-photo"><img src="{{ photo.getPhotoUrl }}" /></div>
这是 getPhotoUrl 方法
public function getPhotoUrl()
{
return '/web/uploads/photos/'.$this->getUserId().'/'.$this->getPhotoUploadDate().'/'.$this->getName();
}
这是 getPhotoUploadDate 方法
public function getPhotoUploadDate() {
return date('Y-m-d', strtotime($this->creationDate));
}
我收到下一个错误 - 警告:strtotime() 期望参数 1 是字符串,给定对象
如果我尝试这种方式
public function getPhotoUrl()
{
return '/web/uploads/photos/'.$this->getUserId().'/'.$this->creationDate.'/'.$this->getName();
}
我收到下一个错误 - 可捕获的致命错误:DateTime 类的对象无法转换为字符串
我做错了什么??
【问题讨论】:
-
错误消息
Object of class DateTime could not be converted to string就是您需要的所有信息。$this->creationDate是一个 DateTime 对象,因此您可以轻松地使用$this->creationDate->format('Y-m-d')代替date('Y-m-d', strtotime($this->creationDate))。为什么要从已有的日期创建日期? -
是的,有效!谢谢!
-
很高兴能帮上忙,我回答了您的问题,如果我的解决方案有效,您可以接受。