How to query data by date using php

Hello, I’m just beginner to learn mongodb,but i have a question about mongodb script
How to query data by date using php

i can query data by date in mongodb using this script

{ Date : { $gt:ISODate(‘2021-01-01’), $lt:ISODate(‘2021-02-30’)}}

here my code in php but It doesn’t work,

$startdate = ‘2021-01-01’;
$stopdate = ‘2021-02-30’;
foreach ($collection->find( [‘Date’ => [‘$gt’=>$startdate ],[‘$lt’=>$stopdate]]) as $doc){
$doc[‘somedata’];
}

  • PHP Version 8.0.1
  • MongoDB 4.4.4 Community

Looks like you are comparing Date to a string right now. Have you looked at this docs page: PHP: MongoDB\BSON\UTCDateTime - Manual?