Symfony2: how to work with datetime objects

I cannot figure out how to work with datetime objects in symfony2.

I have several game records in a database that have a datetime property. and now I want to compare with the actual date: I want to have all the records in the last 10 days. how can i achieve this

I tried this

date($game->getZeit(), mktime(0,0,0,date('m'),date('d'),date('y'))) 

to get a comparable date that I can compare with

 date('Ymd H:i:s', mktime(0,0,0,date('m'),date('d'),date('y'))); 

but it does not work because

 $game->getZeit() 

cannot be converted to string. What for? so how can i debug this one? How can I find out its value? how can i compare it with other dates or date strings?

Quick help would be really appreciated! :)

respectfully

+6
source share
1 answer

In Symfony2 (Doctrine ORM), dates represented as DateTime objects ( http://php.net/DateTime ) Therefore, if $game->getZeit() is an instance of DateTime , you can convert it to a string, for example

 $game->getZeit()->format('Ymd H:i:s'); 
+18
source

All Articles