Skip to content

Instantly share code, notes, and snippets.

@adamtester
Created May 29, 2018 11:42
Show Gist options
  • Save adamtester/c6a134f6fb1634603b109210b8e9f71f to your computer and use it in GitHub Desktop.
Save adamtester/c6a134f6fb1634603b109210b8e9f71f to your computer and use it in GitHub Desktop.
Fix Carbon for SQL Server when using Laravel
/**
* Create a Carbon instance from a specific format.
*
* @param string $format
* @param string $time
* @param \DateTimeZone|string|null $tz
*
* @throws \InvalidArgumentException
*
* @return static
*/
public static function createFromFormat($format, $time, $tz = null)
{
// HACK
if ($format == 'Y-m-d H:i:s.u' && strlen($time) == 19) {
$format = 'Y-m-d H:i:s';
}
// END HACK
if ($tz !== null) {
$dt = parent::createFromFormat($format, $time, static::safeCreateDateTimeZone($tz));
} else {
$dt = parent::createFromFormat($format, $time);
}
$lastErrors = parent::getLastErrors();
if ($dt instanceof DateTime) {
$instance = static::instance($dt);
$instance::setLastErrors($lastErrors);
return $instance;
}
throw new InvalidArgumentException(implode(PHP_EOL, $lastErrors['errors']));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment