ThinkingInBits
ThinkingInBits

Reputation: 11422

How can I compare a DateTime object with doctrine 2.0 DateTime

I'm working on a filter query that will return posts that are between certain dates, however, I'm unable figure out a way to compare the PHP DateTime object with the datetime in the database. The filter component gives me the date in m/d/Y format, and the datetime in the database is in m/d/Y H:i:s format.

$posted_before = \DateTime::createFromFormat("m/d/Y", $posted_before);
$posted_before->format("m/d/Y H:i:s");
$query .= " AND t.posted_date >= $posted_before"; 

I'm using Doctrine 2.0

Upvotes: 0

Views: 2373

Answers (2)

Sebastian Viereck
Sebastian Viereck

Reputation: 5877

there is no need to format the Datetime object, you can use setParameter method:

$qb->where('t.posted_date >= :minDatetime');
$qb->setParameter('minDatetime', $minDatetime);

Upvotes: 0

Wesley van Opdorp
Wesley van Opdorp

Reputation: 14941

Looks to me like you are injecting an object into your string, try storing the return value of format();. Also format the date according to MySQL formatting:

$posted_before = \DateTime::createFromFormat("m/d/Y", $posted_before);
$posted_date = $posted_before->format("Y-m-d H:i:s");
$query .= " AND t.posted_date >= $posted_date"; 

Upvotes: 1

Related Questions