如何从JQuery datepicker到MySQL日期数据类型使用Codeigniter插入值? [英] How to insert values from JQuery datepicker to MySQL date datatype using Codeigniter?

查看:111
本文介绍了如何从JQuery datepicker到MySQL日期数据类型使用Codeigniter插入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用Date数据类型将值从我的JQuery Datepicker插入我的MySQL数据库。如何将字符串转换为日期数据类型? im使用Codeigniter与MVC。这里是我的代码:

i can't insert the values from my JQuery Datepicker to my MySQL database with Date datatype. how will i convert string to date datatype? im using Codeigniter with MVC. here's my code:

javascript

javascript

<script>
$(function() {
    $( "#datepicker" ).datepicker({
        showOn: "button",
        buttonImage: "images/icons/calendar_24.png",
        buttonImageOnly: true,
        onSelect: function(dateText, inst){
            $("input[name='dob']").val(dateText);
        }
    });

});
</script>

控制器

function create()
{
    $data = array(
        'FirstName' => $this->input->post('fname'),
        'DateofBirth' => $this->input->post('dob')
    );

    $this->site_model->add_record($data);
    $this->index();
}

检视

<?php echo form_open('site/create');?>
<p>
    <label for="fname">First Name:</label>
    <input type="text" name="fname" />
</p>

<p>
    <input type="text" name='dob' id="datepicker" />
</p>

谢谢你们!
i在google
中读取一些解决方案,我这样做:

thanks guys! i read some solution in google and i did this:

控制器:

function create()
{
    $date = $this->input->post('dob');

    $data = array(
        'FirstName' => $this->input->post('fname'),
        'DateofBirth' => date('Y-m-d', strtotime($date))
    );

    $this->site_model->add_record($data);
    $this->index();
}

这解决了我的问题! :)

and this solved my problem! :)

推荐答案

我假设你的MySQL表的dob列是 DATE 类型。
日期类型以 yyyy-mm-dd 2012-09-21

I assume your MySQL table's dob column is of DATE type. Date type takes value in the format of yyyy-mm-dd e.g. 2012-09-21

因此,要回答你的问题 - 你需要格式化日期从日期以上有效格式。例如如果你的datepicker日期是 dd-mm-yy 格式,你需要转换为mysql日期格式使用php的 date() function在你的控制器....(还有其他方法来处理像datetime类的日期)

So to answer your question - you need to format the date from the datepicker to the above valid format. e.g. if your datepicker date is in the dd-mm-yy format you need to convert it to mysql date format using php's date() function in your controller .... (there are other methods to handle dates like datetime class too)

function create()
{
    $data = array(
        'FirstName' => $this->input->post('fname'),
        'DateofBirth' => date('Y-m-d', strtotime(str_replace('-', '/', $this->input->post('dob')))); 
    );

    $this->site_model->add_record($data);
    $this->index();
}

使用strtotime() >

NOTE when using strtotime()


m / d / y或dmy格式的日期通过查看
消除各个组件之间的分隔符:如果分隔符a
斜杠(/),则假定美国m / d / y;如果
分隔符是一个破折号( - )或一个点(。),则假定欧洲d-m-y格式

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

这篇关于如何从JQuery datepicker到MySQL日期数据类型使用Codeigniter插入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆