18

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>
iamab.in
  • 2,022
  • 3
  • 18
  • 39
  • Does this answer your question? [change the date format in laravel view page](https://stackoverflow.com/questions/40038521/change-the-date-format-in-laravel-view-page) – jkmartindale Nov 13 '19 at 17:09

7 Answers7

36

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') }}
SamWanekeya
  • 546
  • 5
  • 10
18

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>
13

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
iamab.in
  • 2,022
  • 3
  • 18
  • 39
6

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'))) !!}

enter image description here

vimuth
  • 5,064
  • 33
  • 79
  • 116
4

Try this:

date('d-m-Y', strtotime($user->from_date));

It will convert date into d-m-Y format.

0

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() }} 
Tony
  • 9,672
  • 3
  • 47
  • 75
0

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!

Prasant Kumar
  • 1,008
  • 12
  • 13