我如何在symfony2中将日期对象转换为时间

Alo*_*ain 1 datetime date symfony

我正在使用Symfony2.从数据库中获取日期时,我需要在我的数据库中显示日期index.html.php.

当我打印它的显示如下: DateTime Object ( [date] => 2012-10-09 13:30:23 [timezone_type] => 3 [timezone] => UTC )

但是当我将对象转换为int时它的给出错误: $days2 = floor( $entities->getCreationDate() / (60 * 60 * 24));

Notice: Object of class DateTime could not be converted to int**
Run Code Online (Sandbox Code Playgroud)

jam*_*s_t 6

如果要从DateTime对象获取整数时间戳,请使用getTimeStamp方法.

$date = new DateTime();
echo $date->getTimestamp();
Run Code Online (Sandbox Code Playgroud)


Tho*_*ley 5

我将此添加为第二个答案,因为它与我之前的答案非常不同.

要在PHP中获取两个日期之间的差异,您可能需要考虑使用PHP的内置DateTime和DateInterval类.

以下代码将为您提供创建日期和今天之间的天数差异:

$creationDate = $entity->getCreationDate();
$now = new \DateTime();

$interval = $creationDate->diff($now);

echo "The difference is " . $interval->days . " days.";
Run Code Online (Sandbox Code Playgroud)

有关DateInterval的更多文档:http://www.php.net/manual/en/class.dateinterval.php