I have a date, that prints out as YYYY-MM-DD.
How can I print it out as DD--MM--YEAR, or even better in english: eg 5th May 2018
This is how the date is printed out:
<td>{{$expenses->date}}</td>
I have a date, that prints out as YYYY-MM-DD.
How can I print it out as DD--MM--YEAR, or even better in english: eg 5th May 2018
This is how the date is printed out:
<td>{{$expenses->date}}</td>
You have 3 ways to do this:
1) Using Laravel Model - Doesn't work well with dates below 1900. You can however fix this by making a few adjustments.
<td>{{$expenses->date->format('j F, Y')}}</td>
2) Using PHP strtotime - Doesn't work well with dates below 1900. You can however fix this by making a few adjustments.
{{ date('j F, Y', strtotime($expenses->date)) }}
3) Using Carbon - Works well with all dates so far
{{ \Carbon\Carbon::parse($expenses->date)->format('j F, Y') }}
This is a duplicate of change the date format in laravel view page you can easily doing this.
<td>{{ date('d-M-y', strtotime($expenses->date)) }}</td>
You can make use of date-mutators
Add date field to your model's dates array.
Expense.php
class Expense extends Model
{
protected $dates = ['date', 'another_date_field'];
//other stuff
}
In view file you can format the date field like,
{{ $expenses->date->format('d-m-Y') }} //01-05-2018
or
{{ $expenses->date->format('l jS \\of F Y h:i:s A') }} //Tuesday 1st of May 2018 01:04:00 PM
Try this in blade. It will show nicely with <sup>
tags. Know this is an old thread but adding since it took lots of time for me to find this and wishing it'll be easier to future users.
{!! htmlspecialchars_decode(date('j<\s\up>S</\s\up> F Y', strtotime('21-05-2020'))) !!}
Try this:
date('d-m-Y', strtotime($user->from_date));
It will convert date into d-m-Y format.
The Carbon library is really cool with date formatting in laravel. The topic of String Formatting will be very useful to you.
Use of carbon in blade php can be handled by this format:
{{ \Carbon\Carbon::now()->toDateString() }}
In Laravel you can add a function inside app/Helper/helper.php like
function formatDate($date = '', $format = 'Y-m-d'){
if($date == '' || $date == null)
return;
return date($format,strtotime($date));
}
And call this function on any controller like this
$expenses['date'] = formatDate($date,'d-m-Y');
And in Blade you can write directly
<td>{{$expenses->date}}</td>
Hope it helps!