laravel 更改刀片模板中的日期格式

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

Change date format in blade template

laravel

提问by Hymano

I have a date, that prints out as YYYY-MM-DD.

我有一个日期,打印为 YYYY-MM-DD。

How can I print it out as DD--MM--YEAR, or even better in english: eg 5th May 2018

我如何将其打印为 DD--MM--YEAR,甚至用英语更好:例如 2018 年 5 月 5 日

This is how the date is printed out:

这是打印日期的方式:

          <td>{{$expenses->date}}</td>

回答by

This is a duplicate of change the date format in laravel view pageyou can easily doing this.

这是在 laravel 视图页面更改日期格式的副本,您可以轻松地执行此操作。

<td>{{ date('d-M-y', strtotime($expenses->date)) }}</td>

回答by SamWanekeya

You have 3 ways to do this:

您有 3 种方法可以做到这一点:

1) Using Laravel Model - Doesn't work well with dates below 1900. You can however fixthis by making a few adjustments.

1) 使用 Laravel 模型 - 不适用于 1900 年以下的日期。但是,您可以通过进行一些调整来解决此问题。

<td>{{$expenses->date->format('j F, Y')}}</td>

2) Using PHP strtotime - Doesn't work well with dates below 1900. You can however fixthis by making a few adjustments.

2) 使用 PHP strtotime - 不适用于 1900 年以下的日期。不过,您可以通过进行一些调整来解决此问题。

{{ date('j F, Y', strtotime($expenses->date)) }}

3) Using Carbon - Works well with all dates so far

3) 使用 Carbon - 迄今为止适用于所有日期

{{ \Carbon\Carbon::parse($expenses->date)->format('j F, Y') }}

回答by ab.in

You can make use of date-mutators

您可以使用日期修改器

Add date field to your model's dates array.

将日期字段添加到模型的日期数组。

Expense.php

费用.php

class Expense extends Model
{
    protected $dates = ['date', 'another_date_field'];

    //other stuff
}

In view file you can format the date field like,

在视图文件中,您可以格式化日期字段,例如,

{{ $expenses->date->format('d-m-Y') }} //01-05-2018

or

或者

{{ $expenses->date->format('l jS \of F Y h:i:s A') }} //Tuesday 1st of May 2018 01:04:00 PM

回答by Carlos R. Bécquer Rodríguez

Try this:

尝试这个:

date('d-m-Y', strtotime($user->from_date));

It will convert date into d-m-Y format.

它将日期转换为 dmY 格式。

回答by Aruna Perera

Try this in blade. It will show nicely with <sup>tags. Know this is an old thread but adding since it took lots of time for me to find this and wishing it'll be easier to future users.

在刀片中试试这个。它会很好地显示<sup>标签。知道这是一个旧线程,但添加因为我花了很多时间来找到它并希望它对未来的用户更容易。

{!! htmlspecialchars_decode(date('j<\s\up>S</\s\up> F Y', strtotime('21-05-2020'))) !!}

enter image description here

在此处输入图片说明

回答by Girdharilal Pandey

The Carbon library is really cool with date formatting in laravel. The topic of 'String Formatting' can be much useful to you. So, try to go through this: https://carbon.nesbot.com/docs/

Carbon 库在 laravel 中使用日期格式非常酷。“字符串格式”的主题对您很有用。所以,尝试通过这个:https: //carbon.nesbot.com/docs/

Use of carbon in blade php can be hadled by this format: {{ \Carbon\Carbon::now()->toDateString() }}

在刀片 php 中使用碳可以通过以下格式进行处理:{{ \Carbon\Carbon::now()->toDateString() }}

回答by Prasant Kumar

In Laravel you can add a function inside app/Helper/helper.php like

在 Laravel 中,您可以在 app/Helper/helper.php 中添加一个函数,例如

function formatDate($date = '', $format = 'Y-m-d'){
    if($date == '' || $date == null)
        return;

    return date($format,strtotime($date));
}

And call this function on any controller like this

并在这样的任何控制器上调用此函数

$expenses['date'] = formatDate($date,'d-m-Y');

And in Blade you can write directly

而在 Blade 中你可以直接写

<td>{{$expenses->date}}</td>

Hope it helps!

希望能帮助到你!