0

我知道你可以手动完成,但这不是重点。我需要把今天的日期放在我的 mongoDB 查询中并让它工作。

手动以下工作正常。

$cursor = $collection->find(array("date"=> array('$gt' => '01/10/2013')));

但是当我使用以下

$date = date('d/m/Y');
$cursor = $collection->find(array("date"=> array('$gt' => '$date')));

这没用

我努力了

$cursor = $collection->find(array("date"=> array('$gt' => $date)));
$cursor = $collection->find(array("date"=> array('$gt' => "'".$date."'")));
$cursor = $collection->find(array("date"=> array('$gt' => \"$date\")));

以上都没有奏效

4

1 回答 1

0

这对我来说不合适。而且我认为您的“2013 年 1 月 10 日”标准并没有按照您的预期工作。

“日期”字段如何存储?如果它存储为 MongoDB 日期字段,那么您必须构造一个 MongoDate 对象并在搜索中使用它。

<?php
$insertDate = new MongoDate(strtotime("October 2nd 2013"));
$collection->save(array("date" => $insertDate));

$date = new MongoDate(strtotime("October 1st 2013"));

$collection->find(array("date" => array('$gt' => $date)));

?>

另见http://php.net/mongodate

于 2013-10-21T22:38:24.410 回答