MongoDate toDateTime () function not defined?

This pushes me ... I use an exact example:

http://php.net/manual/en/mongodate.todatetime.php

but I get:

PHP Fatal error: call to undefined method MongoDate :: toDateTime ()

<?php $d = new MongoDate(strtotime("2014-11-18 11:01:25")); var_dump($d); var_dump( $d->toDateTime() ); ?> 

The exact result I get is:

 object(MongoDate)#1 (2) { ["sec"]=> int(1416330085) ["usec"]=> int(0) } PHP Fatal error: Call to undefined method MongoDate::toDateTime() in /xxx/testmongodate.php on line 5 

PHP version:

 $ php -v PHP 5.5.9-1ubuntu4.9 (cli) (built: Apr 17 2015 11:44:57) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies 

PHP configuration:

 $ php -i | grep mongo /etc/php5/cli/conf.d/20-mongo.ini, mongo mongo.allow_empty_keys => 0 => 0 mongo.chunk_size => 262144 => 262144 mongo.cmd => $ => $ mongo.default_host => localhost => localhost mongo.default_port => 27017 => 27017 mongo.is_master_interval => 15 => 15 mongo.long_as_object => 0 => 0 mongo.native_long => 0 => 0 mongo.ping_interval => 5 => 5 

I have successfully used this setup to insert and read from a real mongo database, but now I am trying to work with MongoDate objects, as they are read from mongo, trying to format them to be inserted into another database ... that is why I am looking to use this method. .. but he does not seem to be found ... ??

 $ uname -a Linux server1 3.13.0-52-generic #86-Ubuntu SMP Mon May 4 04:32:59 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux 
+6
source share
1 answer

For anyone who comes to this question late (like me), the probable problem is that PHP uses a different Mongo driver. Currently 2, and the code below is for the old driver. The solution with an outdated driver is to get a date with Mongo Date using the PHP date function:

 <?php $d = new MongoDate(strtotime("2014-11-18 11:01:25")); $newdate = date('Ymd H:m:s',$d->sec); var_dump( $newdate ); ?> 
0
source

Source: https://habr.com/ru/post/987960/


All Articles