dongsiju1941 2016-05-04 16:50
浏览 155

使用MongoDB的PHP中的日期时间格式

I'm getting problems while filtering data from a MongoDB where documents have a "iso_date" field like

ISODate("2010-08-01T00:00:00Z")

in the following .php file,

<?php

$date_a = $_GET['date_a'];
$date_b = $_GET['date_b'];

$m = new MongoClient('mongodb://127.0.0.1:xxxxx');
$db = $m->db;
$collection = $db->collection;

$res  = $collection->find(array(
            'iso_date' => array('$gte' => $date_a, '$lte' => $date_b)
        ));
$m->close();

echo json_encode($res);

?>

where date_a and date_b are created in javascript. I've tried to add string "T00:00:00Z" to YYYY-MM-DD dates, but the result is always empty (and I'm sure it shouldn't be). How can I solve this? I'd like not to convert everything to strings, and to keep a datetime format.

  • 写回答

2条回答 默认 最新

  • dousuiguang9328 2016-05-04 17:10
    关注

    Try this :

    $collection = $db->collection;
    
    $start =  $_GET['date_a'];
    $end = $_GET['date_a'];
    
    // find dates between 1/15/2010 and 1/30/2010
    $collection->find(array("iso_date" => array('$gt' => $start, '$lte' => $end)));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程