i have something problem in there i want to change date format. this my view :
@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 my view and what i want to do:
if someone tell me to use carbon please explain how to use it ?
NB : i use laravel 5.1
You can always use Carbon's ->format('m/d/Y'); to change format. Or you can just use selectRaw to build your queries. Save this answer.
Your answer get current date in jquery in dd/mm/yyyy format” Code Answer. var mm = String(today. getMonth() + 1).
For example, the "d" standard format string indicates that a date and time value is to be displayed using a short date pattern. For the invariant culture, this pattern is "MM/dd/yyyy". For the fr-FR culture, it is "dd/MM/yyyy". For the ja-JP culture, it is "yyyy/MM/dd".
To set and get the input type date in dd-mm-yyyy format we will use <input> type attribute. The <input> type attribute is used to define a date picker or control field. In this attribute, you can set the range from which day-month-year to which day-month-year date can be selected from.
Try this:
<?php echo date('d F Y', strtotime($post->tanggal)); ?>
Where
//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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With