【问题标题】:How to change the date format in laravel view?如何更改 laravel 视图中的日期格式?
【发布时间】:2017-08-06 06:22:44
【问题描述】:

我正在尝试更改 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() }}

请指导我。

【问题讨论】:

标签: php laravel


【解决方案1】:

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

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

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

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

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

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

Carbon Formats

【讨论】:

  • 我试过这样,它还可以 xD,但我猜这是一个复杂的解决方案“{{ Carbon\Carbon::parse(\App\Calllog::orderBy('created_at','desc' )->where('jobseeker_id',$jobseeker->id)->pluck('created_at')->first())->format('j-F-Y') }}"
  • 这个怎么样:{{ \App\Calllog::orderBy('created_at','desc')->where('jobseeker_id',$jobseeker->id)->first() ->created_at->format('j F Y') }} ?
【解决方案2】:

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

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

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

{{$date}}

请参阅文档here

【讨论】:

    【解决方案3】:

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

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

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

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2017-02-23
        • 1970-01-01
        • 2022-12-19
        • 2017-08-29
        • 1970-01-01
        • 1970-01-01
        • 2015-01-06
        • 1970-01-01
        • 2016-09-05
        相关资源
        最近更新 更多