0

Progress :
1. I retireved date from a collection. Example format : Fri Oct 05 14:59:31 +0000 2012
2. I was able to change its format.

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 : Y-m-d G:i:s )

P.S : I did a lot of research on Stackoverflow (And other places, as well.) but I could not come to any conclusions. That is why this question. Let me know if there are any clarifications

4

2 回答 2

1

嗯,即使你已经很好地解释了你的背景,你的实际问题:

在哪里创建一个日期对象,以便它可以以预期格式更新到集合中的文档?(格式:Ymd G:i:s)

有点混乱。

MongoDB 将始终以一种格式保存日期,并且仅在使用以下格式时才保存一种格式ISODatehttp ://en.wikipedia.org/wiki/ISO_8601 (在 PHP 中也称为MongoDate),最好不要混淆这种现状。

所以我建议你Y-m-d G:i:s只使用格式作为显示,即:

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

并且您使用原始$date1对象实际保存到数据库。

当然,如果您要以指定格式创建日期,这会改变,但是这里有一段代码作为示例:

$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));

您可以使用$date2作为日期保存到由您想要的特定格式形成的 MongoDB 中。

于 2013-03-15T09:05:29.873 回答
0

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

您的代码应使用 unix 时间戳创建日期

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

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

于 2013-03-15T09:07:31.423 回答