vatsal shah
vatsal shah

Reputation: 30

mongodb date not matching with the given value

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

Answers (3)

Pushkal Saini
Pushkal Saini

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

Moppo
Moppo

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

Deenadhayalan Manoharan
Deenadhayalan Manoharan

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

Related Questions