日期在DB中显示为“1969-12-31”

时间:2022-11-13 19:18:20

I'm trying to store a date value in a MySQL database using serialize(). However, the result in the db is treated as "1969-12-31". I'm almost certain it's because of the way the data is being serialized in my ajax call.

我正在尝试使用serialize()将日期值存储在MySQL数据库中。但是,db中的结果被视为“1969-12-31”。我几乎可以肯定这是因为我的ajax调用中数据的序列化方式。

Here are the code snippets. Where am I going wrong?

以下是代码段。我哪里错了?

Ajax portion:

data: decodeURIComponent(form.serialize()),

The Result portion of the serialized data is this (when I view the serialized data in console):

序列化数据的Result部分是这样的(当我在控制台中查看序列化数据时):

&pur-date=2014+/+02+/+31

^ I think the "+" is what's causing the error.

^我认为“+”是造成错误的原因。

In my model (Codeigniter):

在我的模型(Codeigniter)中:

$date = date("Y-m-d", strtotime($this->input->post('pur-date')));

If I replace the strtotime value to "2014-10-10" for example, the data is correctly stored into the db. So the issue has to be related to the post data coming in.

例如,如果我将strtotime值替换为“2014-10-10”,则数据将正确存储到db中。所以这个问题必须与后来的数据有关。

Note, column type in db is date.

注意,db中的列类型是date。

Anyone?

2 个解决方案

#1


0  

I'm not entirely sure what's going on because the test data (from the form) is not provided but here are some things I would immediately check for:

我不完全确定发生了什么,因为没有提供测试数据(来自表单),但这里有一些我会立即检查的内容:

  • Javascript counts in milliseconds, Java counts in seconds, PHP counts in seconds: this is your most likely problem.

    Javascript以毫秒为单位,Java以秒为单位计算,PHP以秒为单位计算:这是您最可能遇到的问题。

  • I don't know where 2014-02-31 is coming from. There was no February 31st this year or, for that matter, ever. This could potentially break things?

    我不知道2014-02-31来自哪里。今年2月31日没有,或者就此而言。这有可能破坏事情吗?

#2


0  

So, figured it out - thanks to the comments on my initial question. I was able to remove the white space from my string in my model. Post data was 2014 / 02 / 31, not 2014+/+02+/+31 as was displayed in browser console.

所以,想通了 - 感谢对我最初问题的评论。我能够在模型中删除字符串中的空白区域。发布数据为2014/02/31,而不是2014 + / + 02 + / + 31,如浏览器控制台中所示。

New code:

$format = preg_replace('/\s+/', '', $this->input->post('pur-date')); // formats string and removes whitespace
$date = date("Y-m-d", strtotime($format));

#1


0  

I'm not entirely sure what's going on because the test data (from the form) is not provided but here are some things I would immediately check for:

我不完全确定发生了什么,因为没有提供测试数据(来自表单),但这里有一些我会立即检查的内容:

  • Javascript counts in milliseconds, Java counts in seconds, PHP counts in seconds: this is your most likely problem.

    Javascript以毫秒为单位,Java以秒为单位计算,PHP以秒为单位计算:这是您最可能遇到的问题。

  • I don't know where 2014-02-31 is coming from. There was no February 31st this year or, for that matter, ever. This could potentially break things?

    我不知道2014-02-31来自哪里。今年2月31日没有,或者就此而言。这有可能破坏事情吗?

#2


0  

So, figured it out - thanks to the comments on my initial question. I was able to remove the white space from my string in my model. Post data was 2014 / 02 / 31, not 2014+/+02+/+31 as was displayed in browser console.

所以,想通了 - 感谢对我最初问题的评论。我能够在模型中删除字符串中的空白区域。发布数据为2014/02/31,而不是2014 + / + 02 + / + 31,如浏览器控制台中所示。

New code:

$format = preg_replace('/\s+/', '', $this->input->post('pur-date')); // formats string and removes whitespace
$date = date("Y-m-d", strtotime($format));

相关文章