Reputation: 30
While inserting data into mongodb collection I enter the date as '20-06-2015' and then convert it to mongo format using:
new MongoDate(strtotime(ClearContent(date("Y-m-d",strtotime($start_date)))));
But now when I check in database it shows this:
ISODate("2015-04-19T18:30:00.000Z")
Why does it take the date of yesterday.
Upvotes: 1
Views: 696
Reputation: 11
This will work.
$d = new \MongoDate();
$mongodate = date('Y-m-d H:i:s', $d->sec);
date_default_timezone_set("Asia/Kolkata");
$date = date('Y-m-d H:i:s');
$time = strtotime($date) - strtotime($mongodate);
$mongodate_time = new \MongoDate($d->sec + $time, $d->usec);
Upvotes: 1
Reputation: 19285
Dates in mongodb are stored in UTC, and probably you are using a different timezone in PHP
Try setting this at the beginning of your PHP script to use UTC timezone:
date_default_timezone_set('UTC');
Upvotes: 1
Reputation: 5444
Try this...
$dat = new DateTime(date("Y-m-d",strtotime($start_date)), new DateTimeZone('UTC'));
$get = $dat->getTimestamp();
$date= new MongoDate($get);
Upvotes: 1