Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Fatal error: Class 'MongoDate' not found

I use lithium console (lithium/console/li3) to run some command and I get this error:

PHP Fatal error: Class 'MongoDate' not found

My system details:

  • mongodb server: 2.6.1
  • php mongodb client: 1.5.2
  • apache 2.4.7
  • php 5.5.9-1ubuntu4

$Requests = Requests::find('all', array('conditions'=>array( 'expired'=>array('<'=>new \MongoDate(time())), 'processed'=>0 )));

I don't have this error while running this code in older version system

  • PHP Version 5.3.10-1ubuntu3.11
  • Apache/2.2.22 (Ubuntu) Server
  • mongodb client: 1.4.5
  • mongodb server: 2.4.10

Thank you.

One more thing: I try to create just a simple script

$date = new MongoDate();

it runs without problem via webserver (browser) but if I use php command to run this file, I get the same error: Class 'MongoDate' not found So I believe that it's php command problem.

like image 319
Minh Nguyen Avatar asked May 26 '14 09:05

Minh Nguyen


2 Answers

For people that have come here using PHP7, the classs has been renamed:

MongoDate is now MongoDB\BSON\UTCDateTime

Also I've found that this now wants miliseconds instead of seconds, so make sure you multiple your input by 1000, for example:

$date = new \MongoDB\BSON\UTCDateTime(strtotime('yesterday') * 1000);
like image 141
acidjazz Avatar answered Oct 12 '22 13:10

acidjazz


Make sure your mongodb extension is loaded.

var_dump(extension_loaded('mongodb'));

If not, you must load it in php.ini.

like image 44
Wahyu Kristianto Avatar answered Oct 12 '22 11:10

Wahyu Kristianto