如何在PHP中为MongoDB返回ISO日期格式? [英] How to return ISO date format in PHP for MongoDB?

查看:351
本文介绍了如何在PHP中为MongoDB返回ISO日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将从PHP生成的当前日期作为ISO日期格式存储到MongoDB集合中。

I want to store the current date generated from PHP into MongoDB collection as an ISO date formate.

ISODate("2012-11-02T08:40:12.569Z")

然而,我无法生成这样的日期在php中,将以 ISODate 格式存储在MongoDB中。

However I am not able to generate such Kind of date in php which will be stored in MongoDB as an ISODate format.

这是我做的。

 $d = new MongoDate(time());
 echo $d;

它正在输出如下

0.00000000 1353305590

这不是我需要的格式。如何做?

which is not the format I need. How to do this?

推荐答案

您可以运行 __ toString 或使用 sec 字段

__ toString 将返回时间戳在usecs,您可以传递到 date()在分秒后从毫秒 - 读这里: http://us1.php.net/manual/en/mongodate.tostring.php

__toString will return a timestamp in usecs, which you can pass to date() after separating the seconds from milliseconds - read here: http://us1.php.net/manual/en/mongodate.tostring.php

或,我个人更喜欢mongodb只返回秒,可以直接插入到 date() - 在这里阅读: http://php.net/manual/en/class.mongodate.php

OR, I personally prefer to have mongodb return just the seconds, which can be plugged directly into date() - read here: http://php.net/manual/en/class.mongodate.php

另外,如果您现在正在生成MongoDate(),则不需要指定time();

Also, if you're generating a MongoDate() for right now, you don't need to specify time();

为了返回等值线,您需要执行这个:

In order to return an isodate, you need to do this:

echo date(DATE_ISO8601, (new MongoDate())->sec);

...

$exampleDate = new MongoDate();
echo date(DATE_ISO8601, $exampleDate->sec);

编辑:要保存ISO日期,您需要执行以下操作:

To save your ISO date, you need to do the following:

$mongoDateObject = new MongoDate(strtotime("2012-11-02T08:40:12.569Z"));

这篇关于如何在PHP中为MongoDB返回ISO日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆