Viewing birthdays for the current month in FullCalendar

2

I have a database with a date in the field start , I would like to display only the birthday of the current month in fullcalendar , but I have two difficulties:

  • Select only the birthdays of the current month

  • Show birthdays in the corresponding month in the current calendar year, not the original year of the field.

I currently have this code:

<?php

//Database
$data = array();


$link = mysqli_connect("localhost", "root", "", "agenda");

mysqli_set_charset($link, 'utf8');

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

$query = "SELECT * FROM clientes";

if ($result = $link->query($query)) {

    /* fetch object array */
    while ($obj = $result->fetch_object()) {
        $data[] = array(
            'id' => $obj->id,
            'title'=> $obj->title,
            'start'=> $obj->start
        );
    }

    /* free result set */
    $result->close();
}

mysqli_close($link);

?>

<script>

    $(document).ready(function() {

        $('#clientes').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,basicWeek,basicDay'
            },
            defaultDate: '<?php echo date('Y-m-d');?>',
            editable: true,
            eventLimit: true, // allow "more" link when too many events
            events : <?php echo json_encode($data);?>
        });

    });

</script>

And the view looks like this:

    
asked by anonymous 16.09.2016 / 20:46

1 answer

3

The first part of the problem is solved with the MONTH function of MySQL itself:

SELECT * FROM clientes WHERE MONTH( start ) = MONTH( CURRENT_DATE );
  • MONTH() always returns the current date.

Then, to display the data as a birthday, we have to change the original year for the current year. One of the simplest ways is to make the change in SELECT itself.

Taking advantage of this, we will eliminate the DAY of YEAR and specify exactly what we want:

SELECT
       id,
       CONCAT_WS( '-', YEAR( CURRENT_DATE ), MONTH( start ), DAY( start ) ) AS aniversario,
       titulo
FROM 
       clientes
WHERE
       MONTH( start ) = MONTH( CURRENT_DATE );

So, a date like CURRENT_DATE will be returned as * , appearing in the current month in SELECT .

As we recreate the date using functions, 2010-05-30 served to give us a name in the returned column, then we end with this setting:

while ($obj = $result->fetch_object()) {
    $data[] = array(
        'id' => $obj->id,
        'title'=> $obj->title,
        'start'=> $obj->aniversario
    );
}


If at any point you want to display the calendar for the whole year, just take the 2016-05-30 of the query , because fullcalendar allied with AS aniversário will already separate everything by month of course.


This answer took some good suggestions from @Randrade in the SOpt chat room

    
19.09.2016 / 20:46