Change laravel 5 date format

I have something problem, I want to change the date format. it's my opinion:

@foreach ($posts as $post) <tr> <td> <?php echo $post->id?> </td> <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> </td> </tr> @endforeach 

and my controller:

  public function getIndex() { $posts = DB::table("lembur_karyawan") ->orderBy('id', 'desc') ->paginate(6); return view('user',['posts'=>$posts]); } 

this is my opinion and what I want to do:

enter image description here

If someone tells me to use coal, explain how to use it?

NB: i am using laravel 5.1

+7
date php laravel laravel-5
source share
6 answers

Try the following:

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

Where

  • d - Day of the month (from 01 to 31).
  • F - full text representation of the month (January to December).
  • Y - four-digit number representation of the year
+10
source share
 //replace <td class="mailbox-name"><?php echo $post->tanggal?></td> //to <td class="mailbox-name"><?php echo date('d M Y',strtotime($post->tanggal)); ?></td> 
+7
source share

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

If <?php echo $post->tanggal?> Is a date, then:

This will work:

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

The above returns the same result as you.

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

Carbon inherited from PHP DateTime class. Therefore, if you use laravel, I recommend that you use Carbon.

+6
source share

If you can use Eloquent, you can automatically translate the column into a Carbon date object.

You will need the Post model, and you will need to include tanggal in the columns that are added to the dates.

 class Post extends Model { protected $dates = ['tanggal']; 

Laravel then automatically converts the date to a Carbon object, which gives you access to all the convenient functions.

Then in your view, enter the date only in the format in which you are.

 $post -> tanggal -> format('dm Y'); 

You can learn more about the useful methods Carbon gives you here: http://carbon.nesbot.com/docs/

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

+5
source share

It will 100% solve your problem.

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

use this format

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

http://php.net/manual/en/function.date.php

+4
source share

All Articles