Fadly Dzil
Fadly Dzil

Reputation: 2206

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 :

<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. 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 :

 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. How can I make it true ? Thanks for the help.

Upvotes: 1

Views: 584

Answers (3)

Imtiaz Pabel
Imtiaz Pabel

Reputation: 5443

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;

Upvotes: 0

asfandahmed1
asfandahmed1

Reputation: 464

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();
       }
  });

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31749

Try to change the format with 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 -

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

Upvotes: 1

Related Questions