7

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.

Minh Nguyen
  • 490
  • 1
  • 3
  • 8

2 Answers2

30

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);
acidjazz
  • 1,244
  • 12
  • 14
3

Make sure your mongodb extension is loaded.

var_dump(extension_loaded('mongodb'));

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

Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68