-
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from EmilioBravo/add-trait
Add trait to use in eloquent models
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php namespace Jenssegers\Date\Traits; | ||
|
||
use Jenssegers\Date\Date; | ||
|
||
/** | ||
* This is a trait overwrites methods to | ||
* replace Carbon with Date. | ||
* | ||
* @package Jenssegers\Date | ||
*/ | ||
|
||
trait EloquentDateHelper{ | ||
|
||
public function freshTimestamp() | ||
{ | ||
|
||
return new Date(); | ||
} | ||
|
||
protected function asDateTime($value) | ||
{ | ||
|
||
if (is_numeric($value)) { | ||
|
||
return Date::createFromTimestamp($value); | ||
|
||
} elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) { | ||
|
||
return Date::createFromFormat('Y-m-d', $value)->startOfDay(); | ||
|
||
} elseif (! $value instanceof DateTime) { | ||
$format = $this->getDateFormat(); | ||
|
||
return Date::createFromFormat($format, $value); | ||
} | ||
|
||
|
||
return Date::instance($value); | ||
} | ||
|
||
} |