Laravel中的SQL Server如何格式化日期时间? [英] How format datetime for sql server in laravel?
问题描述
im尝试格式化laravel
中的日期时间值以使其与sql server
格式匹配,我收到此错误:
im trying to format datetime values in laravel
to make them match with sql server
format, im getting this error:
找不到分隔符号.找到意外的数据. 找到意外的数据.追踪数据
The separation symbol could not be found Unexpected data found. Unexpected data found. Trailing data
我在模型中使用自定义格式:
Im using a custom format in my model:
protected $dateFormat = 'd-m-Y H:i:s';
它对于create方法con控制器运行良好,没有问题,尝试更新时会发生错误,我认为与值在数据库中的存储方式有关.
It works good for the create method con controllers, there is no problem, the error happen when try to update and I think is related to how the value is stored in the database.
使用修补程序,我在模型中检索了update_at的值,并以以下格式显示:
Using tinker I retrieve the value of updated_at in my model and is shown in the next format:
updated_at: "2018-07-24 09:14:09.000"
因此,当我更新此值时,使用的格式为:
So when Im updating this value the format used is:
protected $dateFormat = 'd-m-Y H:i:s';
我认为应该是d-m-Y H:i:s.000
之类的东西,但是不起作用,我该如何解决呢?
I think it should be something like d-m-Y H:i:s.000
but doesnt work, how can I solve this?
推荐答案
切换到laravel
版本5.6
时,我遇到了完全相同的问题.现在,我找到了解决此问题的正确方法.
I had exactly the same issue, when switching to laravel
version 5.6
. Now I found the correct way to handle this.
像这样创建一个Base
类,并添加一个public
方法getDateFormat
.
在那里,您返回的日期略有不同
Create a Base
class like this and add a public
method getDateFormat
.
There you return the date a little bit different
return 'Y-d-m H:i:s.v';
请注意,日期(d
)和月份(m
)已切换,最后需要放置一个表示毫秒的.v
.这对我来说很好.在这里看到我完整的Base
类:
See that date (d
) and month (m
) are switched and at the end you need to put a .v
which stands for milliseconds. This worked for me fine. See here my complete Base
class:
<?php
namespace App\Models\Base;
use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model
{
public function getDateFormat()
{
return 'Y-d-m H:i:s.v';
}
public $timestamps = false;
}
这篇关于Laravel中的SQL Server如何格式化日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!