8

i can't really get it how to work with datetime-objects in symfony2.

i have few games-entries in database which one has a datetime-property. and now i want to compare to the actual date: i want to have all records from the past 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 compareable date which i can compare with

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

but that didnt work because

$game->getZeit()

cannot be converted to a string. why? so how can i debug this one? how can i know its value? how can i compare it with other datetimes or date-strings?

quick help would be really appreciated! :)

kind regards

2 Answers 2

21

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

$game->getZeit()->format('Y-m-d H:i:s');
Sign up to request clarification or add additional context in comments.

Comments

0

Let me give another example.

In my case my retrieved data was

array(3) {
  ["full_name"]=>
  string(13) "Administrator"
  ["current_location"]=>
  string(8) "Orogenic"
  ["location_updatedAt"]=>
  object(DateTime)#682 (3) {
    ["date"]=>
    string(26) "2019-09-12 08:21:03.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
}

To get the location_updatedAt value you can use,

$boxInfo['warehouseLocationUpdatedAt']->format('Y-m-d')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.