日期在DB中显示为“1969-12-31”
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.
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):
&pur-date=2014+/+02+/+31
^ I think the "+" is what's causing the error.
In my model (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.
Note, column type in db is date.
Anyone?
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.
New code:
$format = preg_replace('/\s+/', '', $this->input->post('pur-date')); // formats string and removes whitespace
$date = date("Y-m-d", strtotime($format));
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.
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?