user2405604
user2405604

Reputation:

php timezone MongoDB $gt/$lt trouble

i am confused about timezone and mongodate, i understand that mongo used UTC. I need the result in utc+1 for php:

date_default_timezone_get() in php said "Europe/Berlin" which seems correct.

The mongoDate entry is "ISODate("2014-01-06T10:03:47.182Z")", correct for utc +1 is 11:03 not 10:03. So i have trouble with $gt/$lt for group by day.

[EDIT]
My exact problem is, a user registered 0:08 but in mongoDB ISODate it is 23:08 (and another day..), so i have the problem to group exactly the 24 hours.

$dateStart = new MongoDate(strtotime("2014-01-01 00:00:00"));
$dateEnd = new MongoDate(strtotime("2014-12-31 23:59:59"));
array(
array('$match' => array('regdate' => array('$gt' => $dateStart, '$lt' => $dateEnd))),
array('$project' => array('day' => array('$dayOfMonth' => '$regdate'))),
array('$group' => array('_id' => array('day' => '$day'), 'count' => array('$sum' => 1))),
array('$sort' => array('_id.day' => -1)),
);

Thats shows me 1 result, but correct are 2, user registred 0:08 not 23:08.

I hope you understand what i mean. :)

Thanks and best regards,

Upvotes: 1

Views: 457

Answers (1)

Sammaye
Sammaye

Reputation: 43884

MongoDB does not yet understand timezones as such it only implements one, UTC.

This not a bad thing though, really timezones should be implemented in your application not your database.

For this purpose you can use: https://www.php.net/gmdate which uses UTC by default.

You can however, also use date('c', time()); to apply a timezone to dates, I have heard this works but I have not tested it myself. I prefer to let my application deal with timezones that way my database is standardised.

Upvotes: 1

Related Questions