如何在 PHP 中为 MongoDB 返回 ISO 日期格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13448614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:28:14  来源:igfitidea点击:

How to return ISO date format in PHP for MongoDB?

phpmongodbdatetimeisodate

提问by user1518659

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

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

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

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

但是,我无法在 php 中生成此类日期,该日期将作为ISODate格式存储在 MongoDB 中。

This is what I ve done.

这就是我所做的。

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

and it is outputting something like,

它正在输出类似的东西,

0.00000000 1353305590

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

这不是我需要的格式。这该怎么做?

回答by Kavi Siegel

You could run the __toStringfunction, or use the secfield

您可以运行该__toString功能,或使用该sec字段

__toStringwill 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

__toString将在 usecs 中返回一个时间戳,您可以date()在将秒与毫秒分开后传递给它- 在这里阅读:http: //us1.php.net/manual/en/mongodate.tostring.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

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

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

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

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

为了返回一个isodate,你需要这样做:

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

...

...

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

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

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

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

回答by Stas Slabko

For clarity, let's consider the following use case:

为了清楚起见,让我们考虑以下用例:

You need to convert a string in the simplified extended ISO 8601 format(e.g. returned by Javascript's Date.prototype.toISOString()) to and from PHP's MongoDateobject, while preserving maximum precision during conversion.

您需要在简化的扩展 ISO 8601 格式(例如由 Javascript 返回Date.prototype.toISOString())的字符串与 PHPMongoDate对象之间进行转换,同时在转换过程中保持最大精度。

In this format, the string is always 24 characters long: YYYY-MM-DDTHH:mm:ss.sssZ. The timezone is always zero UTC offset, as denoted by the suffix Z.

在这种格式中,字符串总是 24 个字符长:YYYY-MM-DDTHH:mm:ss.sssZ. 时区始终为零 UTC 偏移量,如后缀Z.

To keep milliseconds, we'll have to leverage PHP's DateTimeobject.

为了保持毫秒,我们必须利用 PHP 的DateTime对象。

From string to MongoDate:

从字符串到MongoDate

$stringDt =  "2015-10-07T14:28:41.545Z";

Method 1(using date_create_from_format):

方法一(使用date_create_from_format):

$phpDt = date_create_from_format('Y-m-d\TH:i:s.uP', $stringDt);
$MongoDt = new \MongoDate($phpDt->getTimestamp(), $phpDt->format('u'));

Method 2(using strtotime):

方法2(使用strtotime):

$MongoDt= new \MongoDate(strtotime ($stringDt),
   1000*intval(substr($stringDt, -4, 3)) // cut msec portion, convert msec to usec
);

From MongoDateto string:

MongoDate到字符串

$MongoDt = new \MongoDate(); // let's take now for example
$stringDt =
   substr(
      (new \DateTime())
       ->setTimestamp($MongoDt->sec)
       ->setTimeZone(new \DateTimeZone('UTC'))
       ->format(\DateTime::ISO8601),
   0, -5)  // taking the beginning of DateTime::ISO8601-formatted string
   .sprintf('.%03dZ', $MongoDt->usec / 1000); // adding msec portion, converting usec to msec

Hope this helps.

希望这可以帮助。

回答by manoj tiwari

convert ISO date time in UTC date time here :


$timestamp = $quicky_created_date->__toString(); //ISO DATE Return form mongo database
$utcdatetime = new MongoDB\BSON\UTCDateTime($timestamp);
$datetime = $utcdatetime->toDateTime();
$time=$datetime->format(DATE_RSS);
$dateInUTC=$time;
$time = strtotime($dateInUTC.' UTC');
$dateInLocal = date("d M Y", $time);
echo $dateInLocal; die;