我有4个DateTimes:
$article->getCreated();
$article->getChanged();
$article->getTimestamp();
$article->getImage()->getTimestamp();
我有这样一种方法:
public function getLatestUpdate(\Article $article) { }
有时我的大脑有一个相当高的潜伏期,我没有得到一个很好的解决办法那么快。我可以添加一些if
子句,但是我想要一个更好的方法来return
,这是最新的DateTime values。
我可以在sort()
上使用DateTimes吗?
有一条班轮吗?
发布于 2013-11-19 02:17:58
如果可以将日期包含在数组中,则可以返回最大值值:
$timestamps = array(
$article->getCreated(),
$article->getChanged(),
$article->getTimestamp(),
$article->getImage()->getTimestamp()
);
return max($timestamps);
或者你的一条船:
return max($article->getCreated(), $article->getChanged(), $article->getTimestamp(), $article->getImage()->getTimestamp());
(虽然我认为数组更易读)。
发布于 2013-11-19 02:19:48
发布于 2013-11-19 02:14:04
如果时间戳上的所有日期都可以创建一个数组并获得max
值,那么应该是最后一次更新时间。
https://stackoverflow.com/questions/20068665
复制