【问题标题】:MongoDB / PHP query- if current time/date is in scheduleMongoDB / PHP查询-如果当前时间/日期在计划中
【发布时间】:2012-06-25 21:32:28
【问题描述】:

我有存储事件开始和结束时间的条目。我正在尝试使用 MongoDate 进行查询,以查找查询当前时间发生的所有事件。

这里回答了同样的问题,但对于 MongoDB 而不是 MySQL: PHP / MySQL query - if current time/date is in schedule

到目前为止,我能找到的只是 MongoDate 的反向查询:

$start = new MongoDate(strtotime("2010-01-15 00:00:00"));
$end = new MongoDate(strtotime("2010-01-30 00:00:00"));

// find dates between 1/15/2010 and 1/30/2010
$collection->find(array("ts" => array('$gt' => $start, '$lte' => $end)));

【问题讨论】:

    标签: php mongodb datetime


    【解决方案1】:

    其实很简单:不检查范围,只检查相等:

    $now = new MongoDate();
    $collection->find(array('ts' => $now));
    

    ... 因为看起来您没有将事件开始和结束信息存储在不同的属性中。如果您真的愿意,请使用两个属性:'t_start' 和 't_end',仅在事件实际完成时填充后者。当应用这种方案时,查询变得有点复杂:

    $collection->find(array(
      't_end'   => array('$exists' => false)
    ));
    

    【讨论】:

      猜你喜欢
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 2018-09-16
      • 1970-01-01
      相关资源
      最近更新 更多