Skip to content

Instantly share code, notes, and snippets.

@mariano-aguero
Created October 19, 2015 11:34
Show Gist options
  • Save mariano-aguero/ed7d933e4a86564ba34f to your computer and use it in GitHub Desktop.
Save mariano-aguero/ed7d933e4a86564ba34f to your computer and use it in GitHub Desktop.
Timestamp format trait
<?php namespace App\Models;
class Example extends Controller
{
use TimestampsFormatTrait;
}
<?php namespace App\Models;
use Carbon\Carbon as Carbon;
use Config;
trait TimestampsFormatTrait
{
public function getCreatedAtAttribute($value)
{
return Carbon::parse($value)->toIso8601String();
}
public function setCreatedAtAttribute($value)
{
$this->attributes['created_at'] = Carbon::parse($value)->toDateTimeString();
}
public function getUpdatedAtAttribute($value)
{
return Carbon::parse($value)->toIso8601String();
}
public function setUpdatedAtAttribute($value)
{
$this->attributes['updated_at'] = Carbon::parse($value)->toDateTimeString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment