Created
May 29, 2018 11:42
-
-
Save adamtester/c6a134f6fb1634603b109210b8e9f71f to your computer and use it in GitHub Desktop.
Fix Carbon for SQL Server when using Laravel
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
/** | |
* 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