Nedim
Nedim

Reputation: 573

Format date in a new MongoDB driver for PHP

How can I format a date in new MongoDB driver. This is what var_dump returns:

object(MongoDB\BSON\UTCDateTime)#152 (1) { ["milliseconds"]=> int(1467565836000) }

When I try to access to milliseconds I get the following:

Undefined property: MongoDB\BSON\UTCDateTime::$milliseconds

I'm using a Yii2 framework.

Upvotes: 2

Views: 2729

Answers (2)

dCrystal
dCrystal

Reputation: 604

Since the object of the date is $model->created_at

echo $model->created_at->toDateTime()->format('Y-m-d H:i:s');

The result will be some like:

2016-11-10 16:20:10

Upvotes: 5

Hett
Hett

Reputation: 3813

My formatter for Yii2:

<?php

namespace app\components\i18n;

use MongoDB\BSON\UTCDateTime;

class Formatter extends \yii\i18n\Formatter
{
    protected function normalizeDatetimeValue($value, $checkTimeInfo = false)
    {
        return parent::normalizeDatetimeValue($value instanceof UTCDateTime? 
                 $value->toDateTime()->getTimestamp() : $value, $checkTimeInfo);
    }
} 

Upvotes: 0

Related Questions