Here's what I did for a rough datetime Directive which can accept up to two parameters:
\Blade::directive('datetime', function ($expression) {
$segments = explode(',', preg_replace("/[\(\)]/", '', $expression), 2);
$date = with(trim($segments[0]));
$output = "<?php ";
$output .= "echo (! empty({$date}) and {$date}->timestamp > 0) ? ";
if (count($segments) > 1) {
$format = trim($segments[1]);
$output .= "{$date}->format({$format})";
} else {
$output .= "{$date}->format('m/d/Y')";
}
$output .= " : '';";
$output .= " ?>";
return $output;
});
I can then call it with any of the following (assuming the date is not NULL or 0000-00-00 00:00:00):
@datetime($entity->my_date_col) -> m/d/Y
@datetime($entity->my_date_col, 'Y-m-d') -> Y-m-d
@datetime($entity->my_date_col, 'c') -> ISO 8601 date, ex: 2004-02-12T15:19:21+00:00
@datetime($entity->my_date_col, 'l, F jS, Y \a\t h:i a') -> Friday, October 23rd, 2015 at 01:05 pm
If the date is NULL or 0000-00-00 00:00:00, it will return an empty string.
Edit: I should probably check if $date is an instance of Carbon or DateTime instead of just empty, but I was just sketching out a rough directive so I could stop running these checks in my views and save myself some headache.