Fatal error: Class 'MongoDate' not found when using mongodb php driver 1.1.2 and PHP 7.0.2 - Laravel 5.1 Fatal error: Class 'MongoDate' not found when using mongodb php driver 1.1.2 and PHP 7.0.2 - Laravel 5.1 mongodb mongodb

Fatal error: Class 'MongoDate' not found when using mongodb php driver 1.1.2 and PHP 7.0.2 - Laravel 5.1


As you mentioned you're using the new Mongo extension for PHP 7.

The class names have changed from the older version, i.e.

MongoClient is now MongoDB\Driver\Manager

MongoDate is now MongoDB\BSON\UTCDateTime

I'm not sure how backwards compatible everything is, but this should get you started!


Throughout our app we were regularly converting unix timestamps into MongoDate instances. Example:

new MongoDate(strtotime('-1 day'));

I've therefore created a class to allow conversion between unix timestamps and the new MongoDB\BSON\UTCDateTime, and back

<?phpclass MongoHelper {    const SECONDS_IN_A_MILLISECOND = 1000;    public static function getMongoUTCDateTimeFromUnixTimestamp($timestamp) {        return new \MongoDB\BSON\UTCDateTime(intval($timestamp * self::SECONDS_IN_A_MILLISECOND));    }    public static function getUnixTimestampFromMongoUTCDateTime(\MongoDB\BSON\UTCDateTime $utc_date_time) {        return intval((string) $utc_date_time / self::SECONDS_IN_A_MILLISECOND);    }}

Example usage:

MongoHelper::getMongoUTCDateTimeFromUnixTimestamp(strtotime('-1 day'));