简体   繁体   English

jQuery上的yyyy-mm-dd日期插入到MySQL的DATE中

[英]yyyy-mm-dd date on jquery to insert into DATE in mysql

I have field in my table on the database like this : 我在数据库的表中有这样的字段:

|   date_estimate   |
|   YYYY-mm-dd      |

In php that I use d like this : 在php中,我像这样使用d:

<input type="text" class="datepicker" id="date" value="<?php echo date("d-m-Y"); ?>">

So, I use ajax in jquery to sent the data from my app. 因此,我在jquery中使用ajax从我的应用程序发送数据。 The code is like this : 代码是这样的:

var id = $("#mainTitle strong").text().split("/").pop();
var StringDate = $('#date').val();                     
var cDate = StringDate.split("-");
var DateN = new Date(cDate[2],cDate[1]-1, cDate[0]);
console.log(DateN);

$.ajax({
    url: '<?php echo base_url() . 'control_closing/kasihCatatan/' ?>',
    type: 'POST',
    data: {id: id,
           DateN: DateN,
          },
    dataType: 'json',
    success: function(obj) {
          alert('Update Success');
          location.reload();
       }
  });

In firebug, "console.log" gives me : 在萤火虫中,“ console.log”给了我:

 Date {Thu May 07 2015 00:00:00 GMT+0700 (SE Asia Standard Time)}

So, I failed insert into database. 因此,我无法插入数据库。 How can I solve this ? 我该如何解决? Should I convert date on jquery to YYYY-MM-DD. 我应该将jquery上的日期转换为YYYY-MM-DD。 How can I make it true ? 我该如何实现? Thanks for the help. 谢谢您的帮助。

Try to change the format with php - 尝试使用php更改格式-

var id = $("#mainTitle strong").text().split("/").pop();
var StringDate = $('#date').val(); 
$.ajax({
    url: '<?php echo base_url() . 'control_closing/kasihCatatan/' ?>',
    type: 'POST',
    data: {id: id,
           DateN: StringDate,
          },
    dataType: 'json',
    success: function(obj) {
          alert('Update Success');
          location.reload();
       }
  });

On the php page - 在php页面上-

$date = date('Y-m-d', strtotime($_POST['DateN']));

this should work! 这应该工作!

var id = $("#mainTitle strong").text().split("/").pop();
var StringDate = $('#date').val();                     
var cDate = StringDate.split("-");
var dt = new Date(cDate[2],cDate[1]-1, cDate[0]);
var DateN = dt.getFullYear()+""+(dt.getMonth()+1)+""+dt.getDate();
console.log(DateN);

$.ajax({
    url: '<?php echo base_url() . 'control_closing/kasihCatatan/' ?>',
    type: 'POST',
    data: {id: id,
           DateN: DateN,
          },
    dataType: 'json',
    success: function(obj) {
          alert('Update Success');
          location.reload();
       }
  });

You can try this 你可以试试这个

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!

var yyyy = today.getFullYear();
if(dd<10){
    dd='0'+dd
} 
if(mm<10){
    mm='0'+mm
} 
var today = yyyy+'-'+mm+'-'+dd;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM