Doctrine Mongodb ODM and DateTime query

江枫思渺然 提交于 2019-12-03 16:36:44

This is likely due to this PHP bug which was fixed in 5.3.3:

https://bugs.php.net/bug.php?id=50916

ahmed hamdy

To create query builder for get data when date_last_login great than 5 minutes there 3 ways

1) create DateTime object with your datetime format and get timestamp from DateTime object then create MongoDate object :

$timeBefore5MinutesAgo  = new \DateTime(date('Y-m-d H:i:s',\time() - 5 * 60));
$mongoDateBefore5MinutesAgo = new \MongoDate($currentDateWithTime->getTimestamp());

$query = $this->createQueryBuilder('User')
    ->field('date_last_login')->gte($mongoDateBefore5MinutesAgo)
    ->getQuery();
    ->execute();

2) create MongoDate object and use strtotime to convert you`r datetime format to timestamp :

$mongoDateBefore5MinutesAgo = new \MongoDate(strtotime(date('Y-m-d H:i:s',\time() - 5 * 60)));

$query = $this->createQueryBuilder('User')
    ->field('date_last_login')->gte($mongoDateBefore5MinutesAgo)
    ->getQuery();
    ->execute();

3) only in case Doctrine 2 ODM , you can just create DateTime object with you`r datetime format:

$timeBefore5MinutesAgo  = new \DateTime(date('Y-m-d H:i:s',\time() - 5 * 60));

$query = $this->createQueryBuilder('User')
              ->field('date_last_login')->gte($timeBefore5MinutesAgo)
              ->getQuery();
              ->execute();

all 3 ways will create query this:

db.User.find({ "date_last_login": { "$gte": new ISODate("2014-03-15T19:35:08+02:00") } });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!