Search code examples
phpmongodbphp-mongodb

Fetch Order details between two dates in Mongodb from PHP


First of all I can find some questions asked related to this topic. But I could not able get through. This is more of a solution asked for my code.

I am sending two dates from Ajax with Javascript date.

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);
        }
    });

Controller:

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);
}

Output:

MongoCursor Object ( )

Have tried with ISOString also. Not able to get the output. But when i try the same with Mongo Shell, I am able to get the result.

I dont know what i am missing. It will be a great help if you explain me how to get the result with PHP.

Thanks in advance.


Solution

  • Try this:

    $dateRange = $collection->find(array("timeStamp" => array('$gt' => $start, '$lt' => end)));
    where $start is the start date & $end is the end date