Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
Get dates from database to the jQuery Ui Picker in PHP

I am trying to get the dates from the database using PHP. Which will be highlighted in datepicker. To do achieve that I have made a sql query to fetch the dates as shown below.

HTML

Date: <input type="text" id="datepicker">
// Static dates
// An array of dates
var eventDates = {};
eventDates[ new Date( '08/07/2016' )] = new Date( '08/07/2016' );
eventDates[ new Date( '08/12/2016' )] = new Date( '08/12/2016' );
eventDates[ new Date( '08/18/2016' )] = new Date( '08/18/2016' );
eventDates[ new Date( '08/23/2016' )] = new Date( '08/23/2016' );
<script>
$(function() {
// An array of dates
var eventDates = {};
<?php

$sql = "SELECT * from `table` WHERE `value` > 0";

$result = $connect->query($sql);                       

while($row = $result->fetch_assoc()) { 

?>
eventDates[new Date('<?php echo $date = $row['date']; ?>')] = new Date(
'<?php echo $date = $row['date']; ?>');
<?php
}
?>

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

So in the above code, I have looped all the dates having value > 0. Which should print all the dates but it doesn't. I have checked the query and it works perfectly in a separate PHP file. It echo out all the date have value > 0.

I am not sure if this is the right method to fetch the date from the database and pass it to the datepicker. There is another method using JSON but I am not familiar with that method since I haven't worked with JSON.

What changes can I make to fetch dates from the database and highlight them in the datepicker?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I have formatted the date to YY/MM/DD from YY-MM-DD. Since the datepicker date formate is YY/MM/DD. If you use the second format with - it will not show the output.

Here is the corrected script code:

<script>
    $(function() {
        // An array of dates
        var eventDates = {};
        <?php

        $sql = "SELECT * from `table` WHERE `value` > 0";

        $result = $connect->query($sql);   

        while( $final=$result->fetch_assoc() ) 
        {

        ?>
        eventDates[new Date('<?php 
        $orgDate = date($final['date']);
        $date = str_replace('-"', '/', $orgDate);  
        $newDate = date("Y/m/d", strtotime($date));  
        echo $newDate   ?>')] = new Date(
            '<?php $orgDate =date($final['date']); 
             $date = str_replace('-"', '/', $orgDate);  
             $newDate = date("Y/m/d", strtotime($date));  
             echo $newDate  
             ?>'
        );

        <?php
        
        }

        ?>

        // datepicker
        $('#start').datepicker({

            dateFormat: "yy-mm-dd",

            beforeShowDay: function(date) {
                var highlight = eventDates[date];
                if (highlight) {
                    return [true, "event", 'Tooltip text'];
                } else {
                    return [true, '', ''];
                }
            }
        });
    });
    </script>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda