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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料