php 在 Views laravel 上使用 carbon

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

Use carbon on Views laravel

phplaraveltimeeloquent

提问by jake balba

I want to use the Carbon on Views I'm including it on the top of the views file but it doesnt work, I'm doing it like this.

我想在视图上使用 Carbon 我将它包含在视图文件的顶部,但它不起作用,我正在这样做。

 <?php use carbon/carbon;?>
 @extends('main_layout')

      @foreach ($myquery as $mytask) 
                <tr>

                <td >
                 {{($mytask->firstname)}}
                </td>

                 <td >
                        {{($mytask->lastname)}}
                </td>
                    <td>
               {{($mytask->logon)}}
                    </td>

 @section('content')
 @stop

I just get errors. I want to convert the {{($mytask->logon)}} to human readable format using carbon

我只是收到错误。我想使用 carbon 将 {{($mytask->logon)}} 转换为人类可读的格式

回答by Hammerbot

I would add sommething quoting Laravel Documentationfor googlers to add how you can transform your SQL datetime fields into Carbon objects:

我会为 googlers添加一些引用Laravel 文档的内容,以添加如何将 SQL 日期时间字段转换为 Carbon 对象:

In your Model:

在您的模型中:

protected $dates = ['created_at', 'updated_at', 'disabled_at','mydate'];

All the fields present on this array will be automatically accessible in your views with Carbon functions like:

此数组中存在的所有字段都可以在您的视图中使用 Carbon 函数自动访问,例如:

{{ $article->mydate->diffForHumans() }}

回答by turntwo

You need not add a use statement for carbon in the view. Just make sure that $mytask->logonis indeed a carbon object and use the format()method to turn it into a string

您无需在视图中添加用于 carbon 的 use 语句。只需确保它$mytask->logon确实是一个碳对象并使用该format()方法将其转换为字符串

{{ $mytask->logon->format('Y/m/d') }}

Edit:

编辑:

If $mytask->logonis a carbon object use:

如果$mytask->logon是碳对象使用:

 {{ $mytask->logon->diffForHumans() }}

If it's still a string use:

如果它仍然是字符串,请使用:

{{  \Carbon\Carbon::createFromTimeStamp(strtotime($mytask->logon))->diffForHumans()?? }}

I would advise to do that in the controller though or a view composer to keep your view neat.

我建议在控制器或视图作曲家中这样做,以保持您的视图整洁。

回答by Maniruzzaman Akash

Blade Use:

刀片用途:

{{ \Carbon\Carbon::parse($mytask->logon)->diffForHumans() }}

Output:For a task that is one day ago from now

输出:对于一天前从现在开始的任务

1 day ago

More for Human readable time by Carbon you can read - Carbon Difference For Humans

更多关于碳的人类可读时间,你可以阅读 -人类的碳差异

回答by Sherry

Just Copy Paste it into your Model

只需复制粘贴到您的模型中

public function getCreatedAtAttribute($value)
{
    $date = new Carbon($value);
    return $date->toDayDateTimeString();
}

And called ->created_atlike you normally called in your view.

->created_at像您通常所说的那样称呼您。

Don't forget to use the Carbon Class Model

不要忘记使用碳类模型

回答by Harry Bosh

For laravel 5 Note if you need to do some custom mutations, chuck this in your model.

对于 laravel 5 请注意,如果您需要进行一些自定义更改,请将其放入您的模型中。

 /**
 * The string attribute that should be cast to custom carbon date.
 *
 * @var array
 */

public function getTimeAttribute()
{
     return Carbon::createFromTimestampUTC($this->attributes['time']/1000);
}

Dont worry you can still access the original attribute.

别担心,您仍然可以访问原始属性。

    New = {{ $event->time }} Original = {{ $event->getOriginal('time')}}

Hope this helps someone who cannot use the standard way mentioned.

希望这可以帮助那些不能使用提到的标准方式的人。