php 更改日期格式 laravel 5
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37132266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change date format laravel 5
提问by ivan setiadi
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
注意:我使用 Laravel 5.1
回答by prava
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
- d - 月份中的第几天(从 01 到 31)
- F - 一个月的全文表示(一月到十二月)
- Y - 年份的四位数字表示
回答by ImBhavin95
//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>
回答by Srijan Karki
It is better to use php Carbon
date format in the view file.
最好Carbon
在视图文件中使用 php日期格式。
If <?php echo $post->tanggal?>
is which gets the date then:
如果<?php echo $post->tanggal?>
是获取日期,则:
This will work:
这将起作用:
<?php echo Carbon\Carbon::createFromFormat('Y-m-d', $post->tanggal)->format('d M Y') ?>
<?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 linkmight help.
如果您想进一步更改格式。那么这个链接可能会有所帮助。
Carbon is inherited from PHP DateTime class. So if you are using laravel i recommend you to use Carbon.
Carbon 继承自 PHP DateTime 类。因此,如果您使用 laravel,我建议您使用 Carbon。
回答by Bikash Budhathoki
this will 100 percent solve your issue
这将 100% 解决您的问题
{{ date('jS F Y',strtotime($post->tanggal)) }}
回答by Matt McDonald
If you could use Eloquent, you could automatically cast the column to a Carbon date object.
如果您可以使用 Eloquent,则可以自动将该列转换为 Carbon 日期对象。
You'd need a Post
model and would have to make sure you've included tanggal
in the columns that are cast to dates.
您需要一个Post
模型,并且必须确保您已包含tanggal
在转换为日期的列中。
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.
然后 Laravel 会自动将日期转换为 Carbon 对象,这让您可以访问各种方便的功能。
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/
您可以在此处阅读有关 Carbon 为您提供的有用方法的更多信息: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
您可以在format
此处找到传递的参数中的不同字母:http: //php.net/manual/en/function.date.php
回答by Moauya Meghari
use this format
使用这种格式
echo date('jS F Y',strtotime($post->tanggal));