I am working with the DateTime object and have this problem to obtain the activity of a specific day.
In the controller I do the following query:
$date = new \DateTime('today');
$activity = $em->getRepository('MyBundle:myEntity')->findOneBy(array(
'activity_date' => $date
));
Result for this query is null
, but when I define the parameter date in this way:
$date = new \DateTime('Wednesday, January 14, 2015');
I get the activity that matches this date. Why doesn't today
work?
I believe that commenter @prodigitalson
is right. When you say today
that is in fact now
which is formatted with YYYY-MM-DD HH:MM:SS
.
Now, if your DBMS column is of type date
, DBMS will first normalize two values and only then proceed with comparing.
If your incoming parameter has a value of 2014-01-14 16:47:20
, comparing it to DBMS value of 2014-01-14 00:00:00
will no match the record.
Try the following:
$date = new \DateTime(); // no need for explicit `today`
$date->setTime(0,0,0); // reset hours, minutes and seconds to 0
$activity = $em->getRepository('MyBundle:myEntity')->findOneBy(array(
'activity_date' => $date
));
Will this work?