Skip to content

Instantly share code, notes, and snippets.

@ryanwinchester
Last active October 14, 2017 21:14
Show Gist options
  • Select an option

  • Save ryanwinchester/28a2c7dcc0a251c00b00a98c5a50c794 to your computer and use it in GitHub Desktop.

Select an option

Save ryanwinchester/28a2c7dcc0a251c00b00a98c5a50c794 to your computer and use it in GitHub Desktop.
Carbon helper. Returns a NEW instance of Carbon from input
<?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