提交表单中的 Laravel 日期格式 (dMY)

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

Laravel date format in submission form (d-M-Y)

laraveldatelaravel-4laravel-5

提问by maytham-???????

In the submit form in the blade template I have the following date form and it works fine with default date like Y-m-d.

在刀片模板中的提交表单中,我有以下日期表单,它可以正常使用默认日期,例如Y-m-d.

But I want to show the date like d-M-Y, I have tried to find a usable solution with out luck

但我想显示日期d-M-Y,我试图找到一个可用的解决方案,但运气不佳

Here is the code that works with default date:

这是适用于默认日期的代码:

Here is the model

这是模型

public static $rules = [
    'birthday'   => 'date'
];

protected $fillable = ['birthday'];

Here is the Controller method

这是控制器方法

public function update($id)
{
    $kid = Kid::findOrFail($id);

    $validator = Validator::make($data = Input::all(), Kid::$rules);

    if ($validator->fails())
    {
        return Redirect::back()->withErrors($validator)->withInput();
    }

    $kid->update($data);
    return Redirect::to('kids/list');
}

Here is the Blade template

这是 Blade 模板

{{ Form::model($kid, ['method' => 'PATCH', 'route' => ['kids.update', $kid->id], 'files' => true], ['class' => 'form-horizontal']) }}
{{ Form::text('birthday', null, ['class' => 'form-control']) }}
{{ Form::submit('Confirm update', ['class' => 'btn btn-primary']) }}
{{ Form::close() }}


UPDATE

更新

This works for all Laravel versions from 4.2 and newer.

这适用于 4.2 及更高版本的所有 Laravel 版本。

For Laravel 5.1 and newer, you will need to install it as a separate packageas it's no longer part of the Laravel 5 core.

对于 Laravel 5.1 及更新版本,您需要将其作为单独的包安装,因为它不再是 Laravel 5 核心的一部分。

It was decided that the html/form builder are not a core requirement for the framework so it was removed from the core. The package I linked to is the one that was included with the L4 core and is being maintained as a separate package for L5 compatibility and usage, so you're safe using that if you choose to do so.

决定 html/form builder 不是框架的核心要求,因此从核心中删除了它。我链接到的软件包是包含在 L4 核心中的软件包,并且作为 L5 兼容性和使用的单独软件包进行维护,因此如果您选择这样做,您可以安全地使用它。

Thanks to @Bogdan for this update.

感谢@Bogdan 提供此更新。

回答by Bogdan

If you need one date format for db storage and another for the user interaction, you need to process it before you display it and before you store it into the database. So you should do something like this:

如果您需要一种用于 db 存储的日期格式而另一种用于用户交互的日期格式,则需要在显示之前和将其存储到数据库之前对其进行处理。所以你应该做这样的事情:

1. Convert the date value for your form input:

1. 转换表单输入的日期值:

{{ Form::text('birthday', date('d-M-Y', strtotime($kid->birthday)), ['class' => 'form-control']) }}

2. Before saving convert it to the database required format:

2.保存前将其转换为数据库所需的格式:

public function update($id)
{
    $kid = Kid::findOrFail($id);

    // Convert birthday format to be compatible with database ISO 8601 format
    $data = Input::all();
    $data['birthday'] = date('Y-m-d', strtotime($data['birthday']));

    $validator = Validator::make($data, Kid::$rules);

    if ($validator->fails())
    {
        return Redirect::back()->withErrors($validator)->withInput();
    }

    $kid->update($data);
    return Redirect::to('kids/list');
}

If you want a more object oriented way of handling dates you can use Carbonwhich is already included by Laravel.

如果你想要一种更面向对象的处理日期的方式,你可以使用CarbonLaravel 已经包含的方式。