Beware of the timezone! Working with PHP DateTime and Doctrine for MongoDB can cause conflicts
Doctrine ODM for MongoDB is awesome! Even if you have no experience with NoSQL databases you’ll find the API intuitive and nice to work with.
Dealing with dates though can potentially cause confusion.
I really like Doctrines way of dealing with dates and times. Instead of passing timestamps around or strings with ISO date format it simply makes use of the PHP native type DateTime which provides all you really need to work with.
PHPs interface to MongoDB however comes with its own MongoDate type which provides hardly any functionality compared to DateTime and it’s not compatible with it.
So what’s the difference?
Well MongoDB is not aware of timezones and so is MongoDate. It simply holds the number of seconds passed since the beginning of Unix time to whatever you told it is now.
As long as you are in the GMT timezone (±00:00) you’ll probably never notice but when you are outside of it you can get confusing results especially for times around midnight.
So as a pro tip: check your dates and times for the values you query for and for the values you get as results and compare. If they are just a few hours off you should rework your code.