简体   繁体   English

日期转换和使用PHP更新mongodb文档

[英]Date conversion and updating mongodb document using PHP

Progress : 进度:
1. I retireved date from a collection. 我从一个收藏夹中取出日期。 Example format : Fri Oct 05 14:59:31 +0000 2012 范例格式:2012年10月5日星期五14:59:31 +0000
2. I was able to change its format. 2.我可以更改其格式。

CODE USED : 使用的代码:

         $cur=$col->find(array(),array("created_at"=>1,"_id"=>0));
          // created_at = contains Date value
$cur_all=$col->find();
while($doc=$cur_all->getNext())
{
        $doc2=$cur->getNext();
        $pieces = implode(" ", $doc2);
                    //converted the array to string with space delimiting
        if($pieces!=NULL)
        {
            $date1 = date_create_from_format("D M d G:i:s +O Y", $pieces);
            echo date_format ( $date1 ,  'Y-m-d G:i:s' );
            //This is the format i  would like to update in mongodb..


            $filter = array('_id'=>new MongoId($doc['_id']));
            $update = array('$set'=>array('created_at'=> newMongoDate($date2)));
            $col->update($filter,$update);
        }
}

QUESTION : 题 :

Where to create a date object so that it could be updated to the documents in the collection in the expected format? 在何处创建日期对象,以便可以按预期格式将其更新为集合中的文档? (format : Ymd G:i:s ) (格式:Ymd G:i:s)

PS : I did a lot of research on Stackoverflow (And other places, as well.) but I could not come to any conclusions. PS:我对Stackoverflow(以及其他地方)进行了大量研究,但我无法得出任何结论。 That is why this question. 这就是为什么这个问题。 Let me know if there are any clarifications 让我知道是否有任何澄清

Hmm even though you have explained your background well your actual question: 嗯,尽管您已经很好地解释了背景,但您还是遇到了实际问题:

Where to create a date object so that it could be updated to the documents in the collection in the expected format? 在何处创建日期对象,以便可以按预期格式将其更新为集合中的文档? (format : Ymd G:i:s ) (格式:Ymd G:i:s)

Is a bit confusing. 有点混乱。

MongoDB will always save the date in one format and one format only when using ISODate : http://en.wikipedia.org/wiki/ISO_8601 (otherwise known as MongoDate in PHP) and it is probably best to not mess with this status quo. 仅当使用ISODate时,MongoDB才会始终以一种格式和一种格式保存日期: http : //en.wikipedia.org/wiki/ISO_8601 (在PHP中也称为MongoDate ),最好不要MongoDate这种现状。

So I would recommend you use the format Ymd G:i:s only as display, ie: 因此,我建议您仅将Ymd G:i:s格式用作显示内容,即:

$date1 = new MongoDate();
var_dump(date('Y-m-d G:i:s', $date1->sec));

And you use the original $date1 object to actually save to the database. 然后,您使用原始的$date1对象将其实际保存到数据库。

Of course this would change if you were to create a date in your specified format however here is a piece of code for an example: 当然,如果您要以指定的格式创建日期,这将会改变,但是下面是一个示例代码:

$date1 = new MongoDate();
$date2 = new MongoDate(strtotime(date ( 'Y-m-d G:i:s', $date1->sec )));
var_dump(date('Y-m-d G:i:s', $date2->sec));

You can use the $date2 as the date to save into MongoDB formed from the specific format you want. 您可以使用$date2作为保存到MongoDB的日期,该日期由所需的特定格式组成。

look at http://php.net/manual/en/class.mongodate.php 看看http://php.net/manual/en/class.mongodate.php

your code should create a date using a unix timestamp 您的代码应使用Unix时间戳创建日期

$date2 = ('23rd April 2013');
$update = array('$set'=>array(
                        'created_at'=> new MongoDate(strtotime($date2))
          ));

http://www.php.net/manual/en/function.strtotime.php http://www.php.net/manual/zh/function.strtotime.php

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

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