Support the ongoing development of Laravel.io →
Input Database Forms
Last updated 2 years ago.
0
Solution

Use Carbon. Laravel uses it under the hood for all its date/time related stuff, it makes date/times really easy to work with and works seamlessly with Eloquent.

https://github.com/briannesbitt/Carbon http://laravel.com/docs/4.2/eloquent#timestamps http://laravel.com/docs/4.2/eloquent#date-mutators

Child model

<?php

use Carbon\Carbon;

class Child extends Eloquent {
...

    public function getDates()
    {
        return ['created_at', 'updated_at', 'dob'];
    }

    public function setDobAttribute($value)
    {
        $this->attributes['dob'] = Carbon::createFromFormat('d/m/Y', $value);
    }

...
}

Now when you populate your model with data the dob field is automatically converted into a Carbon object (Make sure you validate input to the needed format), and likewise when you access $child->dob after pulling a model from the DB you automatically have an instance of Carbon to work with.

Last updated 2 years ago.
0

iWader said:

Use Carbon. Laravel uses it under the hood for all its date/time related stuff, it makes date/times really easy to work with and works seamlessly with Eloquent.

https://github.com/briannesbitt/Carbon http://laravel.com/docs/4.2/eloquent#timestamps http://laravel.com/docs/4.2/eloquent#date-mutators

Child model

<?php

use Carbon\Carbon;

class Child extends Eloquent {
...

   public function getDates()
   {
       return ['created_at', 'updated_at', 'dob'];
   }

   public function setDobAttribute($value)
   {
       $this->attributes['dob'] = Carbon::createFromFormat('d/m/Y', $value);
   }

...
}

Now when you populate your model with data the dob field is automatically converted into a Carbon object (Make sure you validate input to the needed format), and likewise when you access $child->dob after pulling a model from the DB you automatically have an instance of Carbon to work with.

Works perfect thanks

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.