php laravel-4 如何更改日期格式

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

laravel-4 how to change date format

phplaravel-4date-formatting

提问by 001221

I want to insert a date into the clients table my db schema is below, I want to insert them into the start_dayand end_dayfields.

我想将日期插入到我的数据库架构在下面的客户端表中,我想将它们插入到start_dayend_day字段中。

I have the below in validations in ClientController.php

我在 ClientController.php 中的验证中有以下内容

If I insert a foreign date_formatother than the one defined below I am thrown an error, but if I do insert the correct one it reverts to 0000-00-00. But if I change the field typeto textfor example the date_formatis inserted fine.

如果我插入的date_format不是下面定义的外来的,我会抛出一个错误,但如果我插入了正确的,它会恢复为0000-00-00. 但是,如果我改变字段typetext例如date_format插入罚款。

$rules = array(
        'project_name'       => 'required',
        'project_brief'      => 'required',
        'start_day' =>  array('required', 'date_format:"m-d-Y"'),
        'end_day'   => array('required', 'date_format:"m-d-Y"')
    );

I'm not sure where the problem lies to be honest. I've even tried to convert the time doing the below:

老实说,我不确定问题出在哪里。我什至尝试转换时间执行以下操作:

$start_day_old = Input::get('start_day');
$start_day = date("d-m-Y", strtotime($start_day_old));
$project = new Project;
$project->start_day = $start_day
$project->save();

However the results were the same. Does anyone know how I can rectify this issue?

然而结果是一样的。有谁知道我该如何解决这个问题?

Clients DB Schema

客户端数据库架构

回答by The Alpha

You can't insert a date formated as dd-mm-yyyyin mysql's datefield, it should be yyyy-mm-dd, so in your code here

你不能插入格式化为一个日期dd-mm-yyyymysqldate字段,它应该是yyyy-mm-dd,所以在你的代码在这里

$start_day = date("d-m-Y", strtotime($start_day_old));

Change it to

将其更改为

$start_day = date("Y-m-d", strtotime($start_day_old));

So, if a date is 15-10-2010then it'll become 2010-10-15and it's a valid date for datefield in the mysqldatabase.

因此,如果日期是,15-10-2010那么它将成为数据库中字段2010-10-15的有效日期。datemysql