I have a @foreach on my blade:
@foreach
{{ $value-> StartDate}}
@endforeach
the StartDate is currently displaying like 'sept 2020 12:00:00AM' I want it do be formatted as 'dd/mm/yy'/'d/m/y' essentially like 23-09-2020 or 23/09/2020
I have tried
{{ $value->StartDate->format('d,m,y')}}
and
{{ Carbon\Carbon::parse($value->StartDate)->format('d,m,y') }}
and also changing 'd,m,y' to 'dd,mm,yyyy' etc but I ma getting error. the main error I get is
Could not parse 'Sep 15 2020 12:00:00:AM': DateTime::__construct(): Failed to parse time string (Sep 15 2020 12:00:00:AM) at position 20 (:): Unexpected character
is there a proper way to change the formatting?
UPDATE with more detail :
Im attempting to change how some dates are displayed on my blade file. they are coming from the sql database as 'Sep 16 2020 12:00:00:AM' i want them to display as '16-09-2020' Below is the attempt I have made, however I am still getting this error
Unexpected data found. Unexpected data found. The separation symbol could not be found Trailing data
Does anyone have any ideas on how to resolve this error?
my model :
class HomeModel extends Entity
{
protected $dates = [
'StartDate',
];
public static function findBy(
\DateTime $StartDate,
\DateTime $EndDate
): array
{
$params = [
$StartDate,
$EndDate,
];
return self::hydrate(DB::select('EXEC Table @StartDate = ?, @EndDate = ?',
$params
));
}
}
my controller :
class Controller extends Controller
{
public function __invoke(Request $request)
{
if (!$request->input('StartDate') && !$request->input('EndDate')) {
$StartTime = Carbon::now()->subDays(8);
$EndTime = Carbon::now();
} else {
$StartTime = Carbon::parse($request->input('StartDate'));
$EndTime = Carbon::parse($request->input('EndDate'));
}
$data = HomeModel::findBy(
Carbon::parse($StartTime),
Carbon::parse($EndTime)
);
return view ('home', compact('data'));
}
my blade :
<tbody class="list">
@foreach($data as $value)
<tr>
<td class="end-time">
{{ \Carbon\Carbon::parse($value->StartDate)->format('d/m/Y')}}
</td>
</tr>
@endforeach
</tbody>
I have attempted the solution given here but it returns the error of
DateTime::__construct() expects parameter 2 to be string, array given
which was expected as I am trying to get the dates from an array. my attempts of turning the array into a string were also futile. Does anyone know of any php functions for formatting dates in an array?