Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change date format laravel 5

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:

enter image description here

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

NB : i use laravel 5.1

like image 413
ivan setiadi Avatar asked May 10 '16 07:05

ivan setiadi


People also ask

How do I change the date format in laravel query?

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.

How can I get current date in jquery DD MMM YYYY format?

Your answer get current date in jquery in dd/mm/yyyy format” Code Answer. var mm = String(today. getMonth() + 1).

What is the format of datetime?

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".

How do I change the date format in input?

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.


4 Answers

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
like image 142
prava Avatar answered Oct 22 '22 02:10

prava


//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>
like image 44
ImBhavin95 Avatar answered Oct 22 '22 01:10

ImBhavin95


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.

like image 31
Srijan Karki Avatar answered Oct 22 '22 01:10

Srijan Karki


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

like image 32
Matt McDonald Avatar answered Oct 22 '22 01:10

Matt McDonald