Skip to content

Instantly share code, notes, and snippets.

@thaenor
Created May 21, 2015 17:13
Show Gist options
  • Save thaenor/5168c6af2b5cf537c514 to your computer and use it in GitHub Desktop.
Save thaenor/5168c6af2b5cf537c514 to your computer and use it in GitHub Desktop.
//BaseModel.php
protected function dateStringToCarbon($date, $format = 'm/d/Y')
{
if(!$date instanceof Carbon) {
$validDate = false;
try {
$date = Carbon::createFromFormat($format, $date);
$validDate = true;
} catch(Exception $e) { }
if(!$validDate) {
try {
$date = Carbon::parse($date);
$validDate = true;
} catch(Exception $e) { }
}
if(!$validDate) {
$date = NULL;
}
}
return $date;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment