mongodb date not matching with the given value

700 views Asked by At

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.

3

There are 3 answers

0
Moppo On BEST ANSWER

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');
0
Deenadhayalan Manoharan On

Try this...

$dat = new DateTime(date("Y-m-d",strtotime($start_date)), new DateTimeZone('UTC'));
$get = $dat->getTimestamp();
$date= new MongoDate($get);
0
Pushkal Saini On

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