Home » Php » php – Change date format laravel 5

php – Change date format laravel 5

Posted by: admin July 12, 2020 Leave a comment


i have something problem in there i want to change date format.
this my view :

  @foreach ($posts as $post)
                      <?php echo $post->id?>
                      <td class="mailbox-star"><?php echo $post->nama?></td>
                      <td class="mailbox-name"><?php echo $post->tanggal?></td>
                      <td class="mailbox-subject"><?php echo $post->deskripsi?></td>
                      <td class="mailbox-date"><?php echo $post->mulai?> - <?php echo $post->durasi?></td>
                      <td class="mailbox-date"><?php echo $post->total?> Jam</td>
                      <td class="mailbox-date">
                      <a href="<?php echo url('user/detail/'.$post->nama)?>" class="btn btn-xs btn-danger"><i class="fa fa-search-plus"></i></a>
                      <a href="<?php echo url('user/edit/'.$post->id)?>" class="btn btn-xs btn-success"><i class="fa fa-pencil-square-o"></i></a>
                      <a href="<?php echo url('user/delete/'.$post->id) ?>" class="btn btn-xs btn-warning"><i class="fa fa-times"></i></a>

and my controller :

    public function getIndex()
        $posts = DB::table("lembur_karyawan")
        ->orderBy('id', 'desc')

        return view('user',['posts'=>$posts]);

this my view and what i want to do:

enter image description here

if someone tell me to use carbon please explain how to use it ?

NB : i use laravel 5.1

How to&Answers:

Try this:

<?php echo date('d F Y', strtotime($post->tanggal)); ?>


  • d – The day of the month (from 01 to 31)
  • F – A full textual
    representation of a month (January through December)
  • Y – A four digit
    representation of a year


<td class="mailbox-name"><?php echo $post->tanggal?></td>
<td class="mailbox-name"><?php echo date('d M Y',strtotime($post->tanggal)); ?></td>


It is better to use php Carbon date format in the view file.

If <?php echo $post->tanggal?> is which gets the date then:

This will work:

<?php echo Carbon\Carbon::createFromFormat('Y-m-d', $post->tanggal)->format('d M Y') ?>

The above returns the same output as you want.

If you want to change the format further. Then this link might help.

Carbon is inherited from PHP DateTime class. So if you are using laravel i recommend you to use Carbon.


If you could use Eloquent, you could automatically cast the column to a Carbon date object.

You’d need a Post model and would have to make sure you’ve included tanggal in the columns that are cast to dates.

class Post extends Model {

    protected $dates = ['tanggal'];

Then Laravel will automatically convert the date into a Carbon object, which gives you access to all sorts of handy functions.

Then within your view just output the date in the format you’re after.

$post -> tanggal -> format('d m Y');

You can read more on the helpful methods Carbon gives you here: http://carbon.nesbot.com/docs/

And you can find out what different letters within the argument passed to format do here: http://php.net/manual/en/function.date.php


this will 100 percent solve your issue

{{ date('jS F Y',strtotime($post->tanggal)) }}


use this format

echo date('jS F Y',strtotime($post->tanggal));