Laravel-mongodb: Wrong DateTime in created_at and updated_at fields in Laravel 5.2

Created on 14 Mar 2016  路  9Comments  路  Source: jenssegers/laravel-mongodb

Hello all,

I'm using Laravel 5.2. I installed an authentication with php artisan make:auth command. I created new user successfully. However, when I checked users' collection in MongoDB database, I saw wrong datetime in created_at and updated_at fields, please refer the following image

How should I do to fix that?

Many thanks for your help,

My env:

  • MongoDB: 3.2.1
  • Laravel 5.2
  • jenssegers/mongodb 3.0.2

Most helpful comment

Me too :(

object(Carbon\Carbon)#231 (3) {
  ["date"]=>
  string(26) "1970-01-01 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "UTC"
}

Laravel 5.4
"jenssegers/mongodb": "^3.2"
MongoDB version: 3.2.7

All 9 comments

Almost the same issue. I'd like to know how to store dates to mongodb. When it comes to create default "created_at" and "updated_at" mongodb sets 1970 year.

Hi @jenssegers , I guess this is a bug.

All created_at and updated_at fields automatically generated wrong value. More issue images

Me too :(

object(Carbon\Carbon)#231 (3) {
  ["date"]=>
  string(26) "1970-01-01 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "UTC"
}

Laravel 5.4
"jenssegers/mongodb": "^3.2"
MongoDB version: 3.2.7

@jenssegers I'm having the same problem.

Laravel 5.3
"jenssegers/mongodb": "^3.0"
MongoDB version: 3.4

Facing same issue, I am using Laravel 5.2 with mongodb 3.2,

I have observed that it was working fine initially but now showing wrong date in db.

Please let me know if any buddy have solution for this.

It seems to work when I override the freshTimestamp() method on the model.

    /**
     * @inheritdoc
     */
    public function freshTimestamp()
    {
        return new UTCDateTime(Carbon::now()->timestamp);
    }

Seems like there was a *1000 on the timestamp originally, which seems to result in a wrong date (Somewhere in 1920 in my case).

Thanks @Lemondragon , but in my case, I did the same with you but the difference is I didn't use Carbon. Mine is mongo 3.2.4 on Laravel 5.4

/** * @inheritdoc */ public function freshTimestamp() { return new UTCDateTime(); }

in youre model
protected $guarded =['created_at','updated_at];

I think its may cause by integer overflow bug.
When save the data using new \MongoDB\BSON\UTCDateTime( round(microtime(true) * 1000));, it save value correctly.

I am running on 32bit version of PHP.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

imrannazirbhat picture imrannazirbhat  路  3Comments

pirmax picture pirmax  路  3Comments

kschethan picture kschethan  路  3Comments

naveedyasin picture naveedyasin  路  3Comments

bastiendonjon picture bastiendonjon  路  3Comments