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);
Related
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.
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 am working with the DateTime object and have this problem to obtain the activity of a specific day.
In the controller I do the following query:
$date = new \DateTime('today');
$activity = $em->getRepository('MyBundle:myEntity')->findOneBy(array(
'activity_date' => $date
));
Result for this query is null, but when I define the parameter date in this way:
$date = new \DateTime('Wednesday, January 14, 2015');
I get the activity that matches this date. Why doesn't today work?
I believe that commenter #prodigitalson is right. When you say today that is in fact now which is formatted with YYYY-MM-DD HH:MM:SS.
Now, if your DBMS column is of type date, DBMS will first normalize two values and only then proceed with comparing.
If your incoming parameter has a value of 2014-01-14 16:47:20, comparing it to DBMS value of 2014-01-14 00:00:00 will no match the record.
Try the following:
$date = new \DateTime(); // no need for explicit `today`
$date->setTime(0,0,0); // reset hours, minutes and seconds to 0
$activity = $em->getRepository('MyBundle:myEntity')->findOneBy(array(
'activity_date' => $date
));
Will this work?
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 am using DateTime class for the first time to convert between different time zones.
Here is what I have
$USER_TIME_ZONE = new DateTimeZone('America/Los_Angeles');
$UTC = new DateTimeZone('UTC');
$schedule_date = new DateTime($call['triggerOn'], $USER_TIME_ZONE);
echo $schedule_date->format('m/d/Y h:i A');
$schedule_date = new DateTime($call['triggerOn'], $UTC);
echo $schedule_date->format('m/d/Y h:i A');
Here is how I am going through my result and trying to convert them
foreach ( $activities AS $call){
$USER_TIME_ZONE = new DateTimeZone('America/Los_Angeles');
$UTC = new DateTimeZone('UTC');
$schedule_date = new DateTime($call['triggerOn'], $UTC);
echo $schedule_date->format('m/d/Y h:i A');
}
The following are value for $call['triggerOn']
2013-02-27 18:00:37
2013-02-02 01:11:07
2013-01-10 17:12:14
2013-02-27 22:29:42
2013-02-27 22:28:38
2013-02-25 21:53:12
2013-02-14 14:35:48
2012-12-13 14:03:16
2013-03-04 19:04:20
2013-03-01 18:52:48
2013-03-05 15:46:56
2013-03-11 15:26:17
2013-02-07 18:17:30
2013-03-05 18:04:25
Both of my outputs are the same! I don't understand why. Is there a configuration that I need to do on the server side as I have PHP running on Windows Server 2008 R2.
Thank you for your help and time.
The DateTime::format() method is returning the time in the timezone the data was created in. There's no conversion going on. Thus your output times are going to be the same as your input times regardless of the timezone you pass in. You can verify this by adding an 'e' to the format parameter. You will see that in the first case the timezone is America/Los_Angeles and in the second it is UTC.
You're probably trying to convert the time between timezones, right? In order to do that just create a single new DateTime object in one timezone, call the setTimezone method with the second timezone, and then format the result.
All of this assumes that the $call['triggerOn'] value is neither a timestamp nor a value with the timezone identified. In that case the second parameter of the DateTime constructor is ignored.
Knowing the value of $call['triggerOn'] would help, but would this work:
$USER_TIME_ZONE = 'America/Los_Angeles';
$UTC = 'UTC';
$schedule_date = new DateTime( $call['triggerOn'], $USER_TIME_ZONE );
echo $schedule_date->format( 'm/d/Y h:i A' );
$schedule_date = new DateTime( $call['triggerOn'], $UTC );
echo $schedule_date->format( 'm/d/Y h:i A' );
Basically, instead of creating new DateTime objects to use as parameters for other new DateTime objects, what if you just use the timezone string instead? Does that work?