简体   繁体   English

如何使用 php 处理 mongoDB 中的“日期时间”?

[英]How can I handle “datetime” in mongoDB with php?

Is there a way to store datetime?有没有办法存储日期时间? So that I can calculate time difference between two datetimes and query with range of datetime.这样我就可以计算两个日期时间之间的时间差并查询日期时间范围。

Also I want to indicate an exact time.我还想指出一个确切的时间。 ex) 12:00:00 01-Jan-2001例如)2001 年 1 月 1 日 12:00:00

Should I store both unix-stamp time and datetime string with timezone or other better ways to handle this problem?我应该将 unix-stamp 时间和 datetime 字符串与时区或其他更好的方法一起存储来处理这个问题吗?

You should use the MongoDate class for storing dates in MongoDB.您应该使用MongoDate class 将日期存储在 MongoDB 中。

From the manual...从手册...

MongoDB stores dates as milliseconds past the epoch. MongoDB 将日期存储为过去纪元的毫秒数。 This means that dates do not contain timezone information.这意味着日期不包含时区信息。 Timezones must be stored in a separate field if needed如果需要,时区必须存储在单独的字段中

You can convert these to DateTime objects by retrieving the sec property and using it in DateTime::setTimestamp() .您可以通过检索sec属性并在DateTime::setTimestamp()中使用它来将这些转换为DateTime对象。 Also see DateTime::setTimezone()另请参阅DateTime::setTimezone()

If you have an application that has to deal with different timezones I will transform all dates to an application timezone before saving them to the db.如果您有一个必须处理不同时区的应用程序,我会将所有日期转换为应用程序时区,然后再将它们保存到数据库。 This way you can do proper calculations.这样您就可以进行适当的计算。 If you need to go back to the original timezone during the application, then save also the name of the original timezone and you will be able to recalculate back.如果您在申请过程中需要将 go 恢复到原始时区,那么您也可以保存原始时区的名称,您将能够重新计算回来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM