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.
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community