Skip to content

Instantly share code, notes, and snippets.

@rajitha-bandara
Last active October 13, 2017 11:23
Show Gist options
  • Save rajitha-bandara/a63a231b30bf9292f5530e1d881d5879 to your computer and use it in GitHub Desktop.
Save rajitha-bandara/a63a231b30bf9292f5530e1d881d5879 to your computer and use it in GitHub Desktop.
Carbon Date Snippets
Format date time using Carbon
\Carbon\Carbon::createFromFormat($from, $value)->format($to);
\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2017-12-10 01:00:00')->format('d/m/Y H:i')
Validate & format Carbon date
if (! function_exists('carbon_date_or_null_if_zero')) {
function convert_datetime_to_carbon($value, $from = 'Y-m-d H:i:s', $to = 'd/m/Y H:i')
{
if(empty($value)){
return null;
}
$carbon_date = \Carbon\Carbon::parse($value);
if($carbon_date->timestamp <= 0){
return null;
}else{
return $carbon_date->format($to);
}
}
Convert Carbon date to string date
$now = \Carbon\Carbon::now();
$now->toDateTimeString();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment