Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between time () and new MongoDate()?

Tags:

php

mongodb

Is there a difference between using PHPs time() and using new MongoDate()? I need to store created_at and updated_at dates for each documents in by mongoDB collection so that I can query them by date (for example documents updated last week).

From what I can see time() and new MongoDate() produces the same result?

like image 566
Jonathan Clark Avatar asked Feb 14 '12 11:02

Jonathan Clark


1 Answers

That's because time() is default for the MongoDate constructor, from the manual:

public MongoDate::__construct ([ int $sec = time() [, int $usec = 0 ]] )

You should use MongoDate objects to query MongoDB.

If you use the raw output of time(), you will store/query for an integer. When you use MongoDate, you will be using MongoDB's Date type which gives you some additional benefits.

like image 152
Mchl Avatar answered Nov 14 '22 14:11

Mchl