I am having trouble formatting a date, it is in this format Mon Jul 30 2018 00:00:00 GMT + 0000 [closed]

-1

I need to save the date and time in the bank, but the date returns me in this format 'Mon Jul 30 2018 00:00:00 GMT + 0000' and the bank is '0000-00-00 00:00:00', I recorded it in a string and it goes out of shape in the bank '1532995200000', I tried to format and make several gambi, but it did not work.

  

AjaxthatsendsthedatatoPHP

$.ajax({type:'POST',data:'title='+title+'&start='+start+'&end='+end+'&allDay='+allDay,url:'agenda/acao.php',success:function(data){$('#title').val('');}});
  

Phpthatreceivesandwritesdata

$title=$_POST['title'];$start=date('d/m/YH:i:s',$_POST['start']);$end=$_POST['end'];try{$stmt=$pdo->prepare("INSERT INTO events (title, start, end, teste) VALUES(?, ?, ?, ?);");
  $stmt->bindValue(1, $title);
  $stmt->bindValue(2, $start);
  $stmt->bindValue(3, $end);
  $stmt->bindValue(4, $start);
  $stmt->execute();
} catch (PDOException $e) {

}
    
asked by anonymous 14.08.2018 / 00:08

1 answer

0

So I realize you're sending a javascript date variable directly. It would be more plausible to format it before sending it to the server. The format that will be stored by% of MySQL is DateTime (formatting in PHP).

Then in the variable Y-m-d H:i:s for example that seems to me a date, before making your ajax request sending it, use the following function to format the date:

formatarData(date) {
    var year = date.getFullYear();

    var mounth = date.getMonth() + 1;
    if (mounth < 10){
        mounth = '0' + mounth;
    }

    var day = date.getDate();
    if (day < 10){
        day = '0' + day;
    }

    var hour = date.getHours();
    if (hour < 10){
        hour = '0' + hour;
    }

    var minutes = date.getMinutes();
    if (minutes < 10){
        minutes = '0' + minutes;
    }

    var seconds = date.getSeconds();
    if (seconds < 10){
        seconds = '0' + seconds;
    }
    return year + '-' + mounth + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds;
}

var dataFormatada = formatarData(start);

Do the same with the other dates and run your ajax request.

$.ajax({
    type: 'POST',
    data: 'title='+title+'&start='+dataFormatada+'&end='+end+'&allDay='+allDay,
    url: 'agenda/acao.php',
    success: function(data){
      $('#title').val('');
    }
  });

On the server, in your PHP file you will not need to format the date, simply assign the variable, since it is already in the format in which it will be stored in the database:

$start = $_POST['start'];
    
14.08.2018 / 00:51