-1

我需要将日期存储在 mysql 中(没有时间)。用户在输入框中输入日期,如 yyyy-mm-dd,可能会在以后更改。你能告诉我在 mongodb 中存储日期的好方法(没有时间),我们会在 mysql 中使用 DATE 类型吗?现在当我需要存储日期和时间时,我使用 mongdb 日期类型。并像这样存储它:

$data['ADDED'] = new MongoDate(time());

并显示它们:

echo gmdate('Y-m-d H:i:s', $data['ADDED']->sec);

当我只使用日期时,我将它们存储为字符串,例如:yyyy-mm-dd(我在存储之前验证日期以确保它是正确的日期)。我需要按日期查找如下内容:

dateField <(>) somedate

您认为在 mongodb 中将日期存储为字符串是否可以接受?你通常如何在mongodb中存储日期?

4

1 回答 1

2

MongoDB does not have a DATE type. It instead has a ISODate type. This is just as good for storing "without" time as I will explain.

So you can use MongoDate like so:

$date = new MongoDate(); // Denotes today, just like the date() function

Now to store without time you can just fake it by making PHP only set a date, as such the default time will be 00:00:00 (I should note this does mean a time is actually stored just as 00:00:00):

$date = new MongoDate(strtotime('2012-04-23')); // strtotime for fun

And then you can query by just the date part like:

find(array('date' => new MongoDate(strtotime('2012-04-23'))));

And you can now query as though you don't have a time since the time will equal what you put in: 00:00:00.

Of course this is just one way of fixing it.

于 2012-12-18T16:15:31.957 回答