Change date format laravel 5

99封情书 提交于 2019-12-21 11:33:15

问题


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


回答1:


Try this:

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

Where

  • 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



回答2:


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



回答3:


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.




回答4:


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




回答5:


this will 100 percent solve your issue

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



回答6:


use this format

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

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



来源:https://stackoverflow.com/questions/37132266/change-date-format-laravel-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!