首先,我可以找到一些与这个主题相关的问题。但是我打不过去。这更像是我的代码的解决方案。
我发送两个日期从Ajax与Javascript日期。
var sd = new Date("2016-10-15T00:00:00.000Z");
var ed = new Date("2016-10-17T00:00:00.000Z");
$.ajax({
type: "GET",
url: "dashboard/load_revenue_chart",
data: {type:"revenue",period:"no of days",start_date: sd.getTime(),end_date: ed.getTime()},
dataType: 'json',
success: function(data){
line_chart(data);
}
});
控制器:
if (isset($_GET['period'])) {
$collection = $this->selectCollection(); // public function for setting the collection.
$start = $_GET['start_date'];
$end = $_GET['end_date'];
print_r($_GET['start_date']); // Outputs : 1476489600000
print_r($_GET['end_date']); // Outputs : 1476662400000
$result = $collection->find(array("OrderedOn" => array('$gt' => $start, '$lt' => $end)));
print_r($result);
}
输出:MongoCursor对象()
也尝试过使用ISOString。无法得到输出。但是当我用Mongo Shell尝试同样的方法时,我能够得到结果。
我不知道我错过了什么。这将是一个很大的帮助,如果你告诉我如何得到的结果与PHP。试试这个:
$dateRange = $collection->find(array("timeStamp" => array('$gt' => $start, '$lt' => end)));
where $start is the start date & $end is the end date
当您使用php使用gine mongo函数在mongo数据库中保存日期时。新的'MongoDB'BSON'UTCDateTime()那么日期格式将保存在DB上,如下所示:
updateTime: ISODate("2020-06-02T07:09:57.099+0000")
当您想获取两个日期之间的所有记录时。例如当前日期和60天。
返回日期使用php然后ex如下:
$milliseconds = round(microtime(true) * 1000);
$dt1 = date("Y-m-d", strtotime("-60 day"));
$dt2 = date("Y-m-d", strtotime("-1 day"));
$where = [ "updateTime" => array('$gte' => new MongoDB'BSON'UTCDateTime(strtotime("$dt1")* 1000), '$lte' => new MongoDB'BSON'UTCDateTime(strtotime("$dt2")* 1000))];
$fetchAllUser = new MongoDB'Driver'Query($where);
$fetchAllUser = $connection->executeQuery($db.".collname", $fetchAllUser);