dongmeng1875 2014-10-15 01:03
浏览 23
已采纳

日期在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?

  • 写回答

2条回答 默认 最新

  • dpo15099 2014-10-15 02:36
    关注

    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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程