Commit 3c0edb03 authored by Jens Segers's avatar Jens Segers

Tweaking date format for json output

parent c2fb55c4
...@@ -136,6 +136,16 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -136,6 +136,16 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
return Carbon::instance($value); return Carbon::instance($value);
} }
/**
* Get the format for database stored dates.
*
* @return string
*/
protected function getDateFormat()
{
return 'Y-m-d H:i:s';
}
/** /**
* Get a fresh timestamp for the model. * Get a fresh timestamp for the model.
* *
...@@ -206,7 +216,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -206,7 +216,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/ */
if ($value instanceof MongoDate) if ($value instanceof MongoDate)
{ {
$value = $this->asDateTime($value)->format('Y-m-d H:i:s'); $value = $this->asDateTime($value)->format($this->getDateFormat());
} }
} }
......
...@@ -318,7 +318,8 @@ class ModelTest extends TestCase { ...@@ -318,7 +318,8 @@ class ModelTest extends TestCase {
public function testDates() public function testDates()
{ {
$user = User::create(array('name' => 'John Doe', 'birthday' => new DateTime('1980/1/1'))); $birthday = new DateTime('1980/1/1');
$user = User::create(array('name' => 'John Doe', 'birthday' => $birthday));
$this->assertInstanceOf('Carbon\Carbon', $user->birthday); $this->assertInstanceOf('Carbon\Carbon', $user->birthday);
$check = User::find($user->_id); $check = User::find($user->_id);
...@@ -327,6 +328,16 @@ class ModelTest extends TestCase { ...@@ -327,6 +328,16 @@ class ModelTest extends TestCase {
$user = User::where('birthday', '>', new DateTime('1975/1/1'))->first(); $user = User::where('birthday', '>', new DateTime('1975/1/1'))->first();
$this->assertEquals('John Doe', $user->name); $this->assertEquals('John Doe', $user->name);
// test custom date format for json output
$json = $user->toArray();
$this->assertEquals($user->birthday->format('U'), $json['birthday']);
$this->assertEquals($user->created_at->format('U'), $json['created_at']);
// test default date format for json output
$item = Item::create(array('name' => 'sword'));
$json = $item->toArray();
$this->assertEquals($item->created_at->format('Y-m-d H:i:s'), $json['created_at']);
} }
public function testIdAttribute() public function testIdAttribute()
......
...@@ -84,4 +84,9 @@ class User extends Eloquent implements UserInterface, RemindableInterface { ...@@ -84,4 +84,9 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
{ {
return $this->email; return $this->email;
} }
protected function getDateFormat()
{
return 'U';
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment