Reputation: 573
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
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
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