Convert AJAX response in array type Date

1

I have an array called eventDates , to which I want to add dates of type Date . The dates I take from a call to a database, but I do not know how to include them in the array in Dates type.

  

This is the script:

var eventDates = {};

$.ajax({
  dataType: 'json',
  method: 'GET',
  url: 'controller/reservas/reservasControllerMostrarFechasReservas.php',
  success: function (res) {
    eventDates= res;
  }
});
  

This is the PHP part:

$consulta = 'SELECT fecha_reserva FROM reservas';

$fecha = parent::query($consulta);

$arrayFechas = array();

while ($fechas = mysqli_fetch_array($fecha)){
  $arrayFechas[] = date('m/d/Y', strtotime($fechas['fecha_reserva']));
}

$verificar_fechas = parent::verificarRegistros($consulta);

for($i=0; $i<$verificar_fechas; $i++){
  echo $arrayFechas[$i];
}

What I want is that in the end it is as if I did this but with the dates that I take the AJAX:

var eventDates = {};
eventDates[ new Date( '08/07/2018' )] = new Date( '08/07/2018' );
eventDates[ new Date( '08/12/2018' )] = new Date( '08/12/2018' );
eventDates[ new Date( '08/18/2018' )] = new Date( '08/18/2018' );
eventDates[ new Date( '08/23/2018' )] = new Date( '08/23/2018' );
    
asked by auremanci 07.03.2018 в 12:41
source

1 answer

0

In the end I answer to myself, since I have found a solution, and if it serves someone here I leave it:

  

Script

// Definimos el array de las fechas
    var eventDates = [];

// Señalamos en el datepicker los días en los que hay alguna reserva realizada (en el CSS es la clase "event a")
$.ajax({
  method: 'GET',
  url: 'controller/reservas/reservasControllerMostrarFechasReservas.php',
  success: function (res) {

    arrayRespuesta = res.split("-");

    for(i=0; i<arrayRespuesta.length; i++){
      eventDates[new Date (arrayRespuesta[i])] = new Date (arrayRespuesta[i]);
    }

    return eventDates;
  }
});

// datepicker
$('#inputFechaReserva').datepicker({
    beforeShowDay: function( date ) {
      var highlight = eventDates[date];
      if( highlight ) {
            return [true, "event", 'Tooltip text'];
      } else {
            return [true, '', ''];
      }
    }
});

});

  

PHP Part

$consulta = 'SELECT fecha_reserva FROM reservas';
$fecha = parent::query($consulta);

$verificar_fechas = parent::verificarRegistros($consulta);

while ($fechas = mysqli_fetch_array($fecha)){
  echo date('m/d/Y', strtotime($fechas['fecha_reserva']));

  if($verificar_fechas > 1){
    echo "-";
    $verificar_fechas = $verificar_fechas - 1;
  }
}
    
answered by 09.03.2018 / 14:13
source