Skip to content

Commit c773be2

Browse files
committed
Improving date conversions
1 parent 76f6200 commit c773be2

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,19 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r
9292
*/
9393
public function fromDateTime($value)
9494
{
95-
// Convert DateTime to MongoDate
96-
if ($value instanceof DateTime)
95+
// If the value is already a MongoDate instance, we don't need to parse it.
96+
if ($value instanceof MongoDate)
9797
{
98-
$value = new MongoDate($value->getTimestamp());
98+
return $value;
9999
}
100100

101-
// Convert timestamp to MongoDate
102-
elseif (is_numeric($value))
101+
// Let Eloquent convert the value to a DateTime instance.
102+
if ( ! $value instanceof DateTime)
103103
{
104-
$value = new MongoDate($value);
104+
$value = parent::asDateTime($value);
105105
}
106106

107-
return $value;
107+
return new MongoDate($value->getTimestamp());
108108
}
109109

110110
/**
@@ -115,25 +115,13 @@ public function fromDateTime($value)
115115
*/
116116
protected function asDateTime($value)
117117
{
118-
// Convert timestamp
119-
if (is_numeric($value))
120-
{
121-
return Carbon::createFromTimestamp($value);
122-
}
123-
124-
// Convert string
125-
if (is_string($value))
126-
{
127-
return new Carbon($value);
128-
}
129-
130-
// Convert MongoDate
118+
// Convert MongoDate instances.
131119
if ($value instanceof MongoDate)
132120
{
133121
return Carbon::createFromTimestamp($value->sec);
134122
}
135123

136-
return Carbon::instance($value);
124+
return parent::asDateTime($value);
137125
}
138126

139127
/**

tests/ModelTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,22 @@ public function testDates()
331331

332332
// test custom date format for json output
333333
$json = $user->toArray();
334-
$this->assertEquals($user->birthday->format('U'), $json['birthday']);
335-
$this->assertEquals($user->created_at->format('U'), $json['created_at']);
334+
$this->assertEquals($user->birthday->format('l jS \of F Y h:i:s A'), $json['birthday']);
335+
$this->assertEquals($user->created_at->format('l jS \of F Y h:i:s A'), $json['created_at']);
336336

337337
// test default date format for json output
338338
$item = Item::create(array('name' => 'sword'));
339339
$json = $item->toArray();
340340
$this->assertEquals($item->created_at->format('Y-m-d H:i:s'), $json['created_at']);
341+
342+
$user = User::create(array('name' => 'Jane Doe', 'birthday' => time()));
343+
$this->assertInstanceOf('Carbon\Carbon', $user->birthday);
344+
345+
$user = User::create(array('name' => 'Jane Doe', 'birthday' => 'Monday 8th of August 2005 03:12:46 PM'));
346+
$this->assertInstanceOf('Carbon\Carbon', $user->birthday);
347+
348+
$user = User::create(array('name' => 'Jane Doe', 'birthday' => '2005-08-08'));
349+
$this->assertInstanceOf('Carbon\Carbon', $user->birthday);
341350
}
342351

343352
public function testIdAttribute()

tests/models/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ public function getReminderEmail()
8787

8888
protected function getDateFormat()
8989
{
90-
return 'U';
90+
return 'l jS \of F Y h:i:s A';
9191
}
9292
}

0 commit comments

Comments
 (0)