Laravel Carbon 格式日期

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

Laravel Carbon format date

phplaraveleloquentphp-carbon

提问by Dev.Wol

I am posting via ajax a date from my form which is in the format.

我通过ajax从我的表单中发布了一个日期格式。

27/07/2016

27/07/2016

When it hits my controller I convert the format to Y-m-d as It is store in my database like that.

当它到达我的控制器时,我将格式转换为 Ymd,因为它像这样存储在我的数据库中。

However I'm getting the following error:

但是我收到以下错误:

DateTime::__construct(): Failed to parse time string (27/07/2016) at position 0 (2): Unexpected character

DateTime::__construct(): 无法解析位置 0 (2) 处的时间字符串 (27/07/2016):意外字符

My controller

我的控制器

public function call(Request $request)
    {              
        return Company->expenses()->where('date_expense', Carbon::parse($request->start)->format('Y-m-d'))->get();
    }

On my expenses model I have the following defined as dates:

在我的费用模型中,我将以下定义为日期:

protected $dates = ['date_expense'];

回答by kfirba

Use Carbon's Carbon::createFromFormat($format, $time, $tz);method:

使用碳的Carbon::createFromFormat($format, $time, $tz);方法:

$start = Carbon::createFromFormat('d/m/Y', $request->start);

return Company->expenses()->where('date_expense', $start)->get();

By the way, there is no need to further format the date as Laravel knows how to handle Carbon objects and will do that automatically for you behind the scenes.

顺便说一句,没有必要进一步格式化日期,因为 Laravel 知道如何处理 Carbon 对象,并且会在幕后自动为您完成。

回答by Jesús Amieiro

Use

Carbon::createFromFormat('d/m/Y', $request->start)->format('d/m/Y')

You have to change your code with this

你必须用这个来改变你的代码

return Company->expenses()->where('date_expense', Carbon::createFromFormat('d/m/Y', $request->start)->format('Y-m-d'))->get();

回答by Martin Ding

If your laravel framework version is 5.0+ you can check by php artisan -V

如果你的 Laravel 框架版本是 5.0+ 你可以检查 php artisan -V

mine is Laravel Framework 5.4.19you can make a reference from laravel document https://laravel.com/docs/5.4/eloquent-mutators#date-mutators

我的是Laravel Framework 5.4.19你可以从 laravel 文档https://laravel.com/docs/5.4/eloquent-mutators#date-mutators做一个参考

The following is an example:

下面是一个例子:

  1. if your laravel Framework doesn't contains Carbon ,you can run

    composer require nesbot/carbon

  2. at your model such as Post model

  1. 如果你的 laravel 框架不包含 Carbon ,你可以运行

    composer require nesbot/carbon

  2. 在您的模型上,例如 Post 模型

class Post extends Model`
{
    protected $fillable = ["title","body"];`

    protected $dates = [`
        'created_at',`
        'updated_at',`
        'deleted_at'`
    ];
}
  1. use tinker to add a post open terminal under project directory run php artisan tinker
  1. 使用 tinker 在项目目录 run 下添加一个 post open 终端 php artisan tinker

App\Post::create("title"=>"This is Title","body"=>"this is body")

App\Post::create("title"=>"This is Title","body"=>"this is body")

and then you can get lastest created post's created_at by Carbon

然后你可以得到最新创建的帖子的 created_at by Carbon

by App\Post::latest()->get()->created_at->diffForHumans()

经过 App\Post::latest()->get()->created_at->diffForHumans()

you can also use same method in blade like this {{ $post->created_at->diffForHumans() }}

您也可以像这样在刀片中使用相同的方法 {{ $post->created_at->diffForHumans() }}

the Carbon document url is as followed: http://carbon.nesbot.com/docs/

Carbon 文档 url 如下:http: //carbon.nesbot.com/docs/