【问题标题】:put todays date in array将今天的日期放入数组中
【发布时间】:2013-10-15 05:55:33
【问题描述】:

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

以上方法均无效

【问题讨论】:

标签: php arrays mongodb date datetime


【解决方案1】:

这对我来说不合适。而且我认为您的“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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-29
  • 2015-12-24
  • 1970-01-01
相关资源
最近更新 更多