I’m having difficulty formatting a date, it is in this format Mon Jul 30 2018 00:00:00 GMT+0000

Asked

Viewed 57 times

-1

I need to record 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 in the bank is '0000-00-00 00 00:00', I recorded the same in a string and it is disarranged in the bank '1532995200000', I tried to format and make several Ambi, but it did not work.

inserir a descrição da imagem aqui

Ajax sending data to PHP

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

Php that receives and writes data

$title = $_POST['title'];
$start = date('d/m/Y H: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) {

}
  • Could [Edit] the question and add the code?

  • In which database you are trying to record, and what type of field you are using to store that date?

  • Try to use the STR_TO_DATE mysql.

  • Anderson Carlos Woss - This edited

  • Pedro Souza Mysql Field Datetime

1 answer

0


From what I noticed you are 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 DateTime of Mysql is Y-m-d H:i:s (formatting in PHP).

Then on the variable start for example it 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 thing with the other dates and execute 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 it will not be necessary to format the date, simply assign the variable, because it is already in the format in which it will be stored in the database:

$start = $_POST['start'];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.