I have a mongodb record like "estdate" : "2016-06-14T06:49:00.000Z" I am writing the following php code to query the time
$from=$start = new MongoDate(strtotime('2016-04-11 00:00:00'));
$to=new MongoDate(strtotime('2016-06-18 00:00:00'));
$filterpatient = array("order.patientinfo.order_id" => $ordertext,"order.orderitem.estdate" => array('$gte' =>$from, '$lte' => $to));
$cursor = $collection->find($filterpatient)->sort(array('_id'=>-1));
But this is giving no result.
If you are using mongodb driver, this might be helpful for you.
$starttime = strtotime("2016-04-11 00:00:00");
$utcstartdatetime = new MongoDB\BSON\UTCDateTime($starttime);
Now use the $utcstartdatetime in your query for time.
Related
I am writing a query in which I want to fetch data against created_at column. But its not working, it does not return anything. I am using Mongodb as database. Created_at column date is in the format "created_at": ISODate("2016-05-11T09:29:33.112Z") and when I fetch it from database it gives me 2016-05-11 09:29:33.
$datetimenow = date('Y-m-d H:i:s');
$data = ChatMessages::where('created_at','<',$datetimenow)->count();
Try using Carbon Package
$now = \Carbon\Carbon::now('Asia/Dubai'); //you can specify your own timezone
$messages = ChatMessages::where('created_at', '<', $now)->get();
dd($messages);
I got the solution, this is the way how I can do this.
$d1 = new MongoDate(strtotime('2016-01-04 23:21:46'));
$d2 = new MongoDate(strtotime('2016-05-09 23:21:46'));
$data = ChatMessages::whereRaw(['created_at' => array('$gt' => $d1, '$lt' => $d2)])->count();
I need to convert this query from php to mongoDB query
$query = "select * from table where data_added like '%data%';
I have date stored in variable
$date = "2013-09-02";
and in my mongo Document the date sorted as :
$dateAdded = new MongoDate(strtotime('2013-09-02 12:21:55'));
I tried
$date = new MongoDate(strtotime("$date"));
$mongo->find(array('date_added'=>array('$lt'=>$date)));
and
$mongo->find(array('date_added'=>$date));
but without success .
so I need to query usin (Y-m-d) not (Y-m-d h:i:s)
so how to use LIKE query for data in mongo
Thanks
You need to do a range query. Create a timestamp, for example using strtotime(), to get the unix timestamp at the start of the day, and another one and the end of the day.
Depending on if you want these two ends inclusive or exclusive, you then use
// Both points/seconds inclusive
->find(array("date" => array('$gte' => $startOfDay, '$lte' => $endOfDay)));
// Both seconds exclusive
->find(array("date" => array('$gt' => $startOfDay, '$lt' => $endOfDay)));
See http://cookbook.mongodb.org/patterns/date_range/
I want to insert a date into a collection.
I use the class MongoDate to create the date object:
$today = new MongoDate(strtotime(date('Y-m-d 00:00:00')));
The problem is that once it is in my collection the date is 2 hours earlier.
For instance, $today here should be 2013-05-28 00:00:00 but once in the database it is 2013-05-27 22:00:00.
I can't resolve this problem by adding 2 hours manually to the timestamp because I use the date in queries.
The local time of the server where Mongo is running is set to the correct time of my country.
That works in the new php version of mongodb:
new MongoDB\BSON\UTCDateTime((new DateTime($today))->getTimestamp()*1000)
$dt = new DateTime(date('Y-m-d'), new DateTimeZone('UTC'));
$ts = $dt->getTimestamp();
$today = new MongoDate($ts);
This is working.
Remove old document and insert
$bill = array(
"_id" => 1,
"name" => "A",
"lastModified" => new MongoDate()
);
$collection->insert($bill);
FYI: If you need date created for your object model.
$date_created = new \MongoDB\BSON\UTCDateTime(time()*1000);
How in PHP do I get the regular timestamp format out of a MongoDB date?
Assume I have:
$my_date;
print_r($my_date);
The print_r output is:
MongoDate Object ( [sec] => 1346300336 [usec] => 593000 )
But doing:
echo $my_date;
Outputs:
0.59300000 1346300336
Even tried:
echo (string)$my_date
Same thing.
$my_date->sec is the unix timestamp, use date() function to show it in required format.
echo date('Y-m-d H:i:s', $my_date->sec);
Just a quick update, to say that MongoDate has a toDateTime method since the version 1.6 of the pecl extension.
You can now do
$mongoDate->toDateTime()->format(...)
you are missing the microsecond.
To show (mongo -> php)
$fecha = date(preg_replace('`(?<!\\\\)u`', $my_date->usec, 'Y-M-d H:i:s.u'), $my_date->sec);
//MongoDate ISODate("2013-05-28T15:27:24.735Z")
//Php Date 2013-May-28 10:27:24.735000
To send to mongo (php -> mongo)
$fecha_mongo = new MongoDate(strtotime($fecha));
//Fail function, the short way but, 70000 isn't equal to 700000.
//$fecha_mongo->usec = (int)$fecha_micro->format("u");
preg_match("/\.(.*)/", $fecha, $uSec);
$fecha_mongo->usec = (int)(count($uSec)==2?$uSec[1]:0);
//Php Date 2013-May-28 10:27:24.735000
//MongoDate ISODate("2013-05-28T15:27:24.735Z")
Good day!
Mario T.
First, create date from millisecond using given function:
public function showdatefn($mili)
{
$seconds = (string)$mili / 1000;
return date("d-m-Y", $seconds);
}
$date =$this->showdatefn(<put mongo date here>);
This will give you correct date.
Use MongoDB\BSON\UTCDateTime MongoDate is deprecated
$mongoDate = new \MongoDB\BSON\UTCDateTime(strtotime('2020-10-01 18:30:00'));
Being relatively new to mongo, I read in the mongo manual about Optimizing Object IDs.
http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs#OptimizingObjectIDs-Extractinsertiontimesfromidratherthanhavingaseparatetimestampfield.
Going with the recommendation about NOT creating a separate Created_On field, I decided I would later extract out the date from the _id field which is an ObjectID
Now, I have many records all with out a Created_On field.
I am attempting to query for a date range, but am unsure of the syntax:
$start = new MongoDate(strtotime("2012-03-01 00:00:00"));
$end = new MongoDate(strtotime("2012-03-15 00:00:00"));
$collection->find(array('_id' => array('$gt' => $start, '$lte' => $end)));
Always returns 0 results. Although I can query individual records and extract the date from the object.
In PHP, you need to do something like:
function timeToId($ts) {
// turn it into hex
$hexTs = dechex($ts);
// pad it out to 8 chars
$hexTs = str_pad($hexTs, 8, "0", STR_PAD_LEFT);
// make an _id from it
return new MongoId($hexTs."0000000000000000");
}
$start = strtotime("2012-03-01 00:00:00");
$end = strtotime("2012-03-15 00:00:00");
$collection->find(array('_id' => array('$gt' => timeToId($start), '$lte' => timeToId($end))));
Then you can use that to query the _id field.
I wrote a blog post describing the process here: http://www.snailinaturtleneck.com/blog/2011/12/20/querying-for-timestamps-using-objectids/