把今天的日期放在数组中

时间:2013-10-15 05:55:34

标签: php arrays mongodb date datetime

我知道你可以手工做到这一点,但这不是重点。我需要将今天的日期放在我的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\")));

以上都没有工作

1 个答案:

答案 0 :(得分:0)

这对我来说不合适。我认为你的'01 / 10/2013'标准并不像你预期的那样运作。

如何存储“日期”字段?如果它存储为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