Inserting a date in mongodb

前端 未结 4 618
臣服心动
臣服心动 2020-12-31 06:55

I want to insert a date into a collection. I use the class MongoDate to create the date object:

$today = new MongoDate(strtotime(date(\'Y-m-d 00         


        
相关标签:
4条回答
  • 2020-12-31 07:18

    Remove old document and insert

            $bill = array(  
                    "_id" => 1, 
                    "name" => "A", 
                    "lastModified" => new MongoDate()
                );
    
            $collection->insert($bill);
    
    0 讨论(0)
  • 2020-12-31 07:20
    $dt = new DateTime(date('Y-m-d'), new DateTimeZone('UTC'));
    $ts = $dt->getTimestamp();
    $today = new MongoDate($ts);
    

    This is working.

    0 讨论(0)
  • 2020-12-31 07:27

    FYI: If you need date created for your object model.

    $date_created = new \MongoDB\BSON\UTCDateTime(time()*1000);
    
    0 讨论(0)
  • 2020-12-31 07:32

    That works in the new php version of mongodb:

    new MongoDB\BSON\UTCDateTime((new DateTime($today))->getTimestamp()*1000)
    
    0 讨论(0)
提交回复
热议问题