提交表单中的 Laravel 日期格式(d-M-Y) [英] Laravel date format in submission form (d-M-Y)

查看:22
本文介绍了提交表单中的 Laravel 日期格式(d-M-Y)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

这是使用默认日期的代码:

Here is the code that works with default date:

这是模型

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

protected $fillable = ['birthday'];

这是控制器方法

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');
}

这是 Blade 模板

Here is the Blade template

{{ 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() }}


更新

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

This works for all Laravel versions from 4.2 and newer.

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

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

决定 html/form 构建器不是框架的核心要求,因此将其从核心中删除.我链接到的包是 L4 核心中包含的包,它作为一个单独的包进行维护,以实现 L5 兼容性和使用,因此如果您选择这样做,您可以安全地使用它.

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.

感谢@Bogdan 提供此更新.

Thanks to @Bogdan for this update.

推荐答案

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

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:

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

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

<强>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');
}

如果您想要一种更加面向对象的方式来处理日期,您可以使用 Carbon包含在 Laravel 中.

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

这篇关于提交表单中的 Laravel 日期格式(d-M-Y)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆