如何在 Laravel 视图中更改日期格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45529200/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:27:41  来源:igfitidea点击:

How to change the date format in laravel view?

phplaravel

提问by Soul Coder

I am trying to change the date format in laravel blade, I want to get 14 july 2017 but i am getting like 2017-07-14 06:04:08.Here is how i tried to retrive the created_date column.

我正在尝试更改 Laravel Blade 中的日期格式,我想要 2017 年 7 月 14 日,但我得到了2017-07-14 06:04:08 。这是我尝试检索 created_date 列的方式。

  {{ \App\Calllog::orderBy('created_at','desc')->where('jobseeker_id',$jobseeker->id)->pluck('created_at')->first() }}

Please guide me.

请指导我。

回答by yoeunes

Laravel created_at is a Carbon instance, just check Carbon documentation to format the date as you want :

Laravel created_at 是一个 Carbon 实例,只需查看 Carbon 文档以根据需要格式化日期:

echo $created_at->format('j F Y');        
 // 14 july 2017

or if you're getting just a string parse it and then format it :

或者如果你只是得到一个字符串解析它然后格式化它:

Carbon\Carbon::parse($created_at)->format('j F Y')

i think it should work like this :

我认为它应该像这样工作:

{{ \App\Calllog::orderBy('created_at','desc')->where('jobseeker??_id',$jobseeker->id)??->first()->created_a??t->format('j F Y') }} 

Carbon Formats

碳格式

回答by Umang Patel

use below code in your blade file

在您的刀片文件中使用以下代码

{{ date_format(date_create("your date variable"),'d M Y') }}

or if your date variable is already an date object then

或者如果您的日期变量已经是日期对象,那么

{{ date_format($variable,'d M Y') }}

回答by Bikash Budhathoki

This will 100 percent solve your issue..

这将 100% 解决您的问题..

{{\App\Calllog::orderBy('created_at','desc')->where('jobseeker_id',$jobseeker->id)->first()->created_ar->format('jS F Y')}}

{{\App\Calllog::orderBy('created_at','desc')->where('jobseeker_id',$jobseeker->id)->first()->created_ar->format('jS F Y') }}

回答by Sagar Gautam

You can use Carbon for converting Date Timeto your desired format.

您可以使用 Carbon 将日期时间转换为您想要的格式。

<?php
    $date = \Carbon\Carbon::parse($datetime)->format('dd-MM-yy');
?>

Now, use this $datevariable to display time like:

现在,使用这个$date变量来显示时间,如:

{{$date}}

See docs here.

请参阅此处的文档。