Skip to content

Handle MongoDB date only  #1

@nunomazer

Description

@nunomazer

Based on code:

use Jenssegers\Mongodb\Eloquent\Model;

class Etapa extends Model {
    protected $dates = ['data_inicio', 'data_termino'];

    public function getDataInicioAttribute($value) {
        return \Carbon\Carbon::createFromTimestamp($value->toDateTime()->getTimestamp())->timezone('UTC');
    }

    public function getDataTerminoAttribute($value) {
        return \Carbon\Carbon::createFromTimestamp($value->toDateTime()->getTimestamp())->timezone('UTC');
    }

    public function setDataInicioAttribute($value) {
        $value = \DateTime::createFromFormat('d/m/Y', $value, new \DateTimeZone('UTC'));
        $value->setTime(0, 0, 0);

        $this->attributes['data_inicio'] = new \MongoDB\BSON\UTCDateTime($value);
    }

    public function setDataTerminoAttribute($value) {
        $value = \DateTime::createFromFormat('d/m/Y', $value, new \DateTimeZone('UTC'));
        $value->setTime(0, 0, 0);

        $this->attributes['data_termino'] = new \MongoDB\BSON\UTCDateTime($value);
    }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions