Last active
October 14, 2017 21:14
-
-
Save ryanwinchester/28a2c7dcc0a251c00b00a98c5a50c794 to your computer and use it in GitHub Desktop.
Carbon helper. Returns a NEW instance of Carbon from input
This file contains hidden or 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
| <?php | |
| use Carbon\Carbon; | |
| if (! function_exists('carbonize')) { | |
| /** | |
| * @param mixed $time | |
| * @param string $tz | |
| * @return Carbon | |
| * @throws InvalidArgumentException | |
| */ | |
| function carbonize($time = null, $tz = 'UTC') | |
| { | |
| switch (true) { | |
| case is_null($time): | |
| return Carbon::now($tz); | |
| case $time instanceof Carbon: | |
| return $time->copy(); | |
| case $time instanceof DateTime: | |
| return Carbon::instance($time); | |
| case $time instanceof DateTimeImmutable: | |
| return Carbon::instance(new DateTime($time->format(DateTime::ATOM))); | |
| case is_numeric($time) && (string) (int) $time === (string) $time: | |
| return Carbon::createFromTimestamp((int) $time, $tz); | |
| case is_string($time) && strtotime($time) !== false: | |
| return Carbon::parse($time, $tz); | |
| default: | |
| throw new InvalidArgumentException("I don't know what to do with this."); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment