Sixdas Sixdas - 2 months ago 6
PHP Question

Repeat array date in php for fullcalendar

I have to be able to view on fullcalendar.io a series of shakes that are repeated.

With php I managed, to repeat an array, now I have difficulty display it on the calendar.

Here is the code

$aDate = '2016-09-20';
$rounds = array('Sera', 'Pomeriggio', 'Mattina', 'Notte', 'Riposo');

for($i=0; $i<20; $i++) {
$nextDate = date("Y-m-d", strtotime('+1 day', strtotime($aDate)));
echo "\n<br>" . $nextDate . " " . $rounds[$i%count($rounds)];
$aDate = $nextDate;
}


Now for viewing on full calendar I have to create a similar array like this:

events: [
{
title : 'event1',
start : '2010-01-01'
},
{
title : 'event2',
start : '2010-01-05',
}
]


I have tried in this world but it does not work. Give me a hand to solve.

$aDate = '2016-09-20';
$rounds = array('Sera', 'Pomeriggio', 'Mattina', 'Notte', 'Riposo');
for($i=0; $i<20; $i++) {
$nextDate = date("Y-m-d", strtotime('+1 day', strtotime($aDate)));

$event_array[] = array(
'id' => '1',
'title' => $rounds[$i%count($rounds)],
'start' => $nextDate,
'color' => '#000'
);

$aDate = $nextDate;
}


thank you

Answer

Please Check below code [NOTE: I have managed to make a similar structure as you mentioned, you can changed according to satisfy full calendar needs]

<?php

$aDate = '2016-09-20';
$rounds = array('Sera', 'Pomeriggio', 'Mattina', 'Notte', 'Riposo');

$events = array();

for($i=0; $i<20; $i++) {
    $nextDate = date("Y-m-d", strtotime('+1 day', strtotime($aDate)));
    $event_array = array();
    $event_array['id'] = $i+1;
    $event_array['title'] = $rounds[$i%count($rounds)];
    $event_array['start'] = $nextDate;
    $event_array['color'] = '#000';

    $aDate = $nextDate;

    $events[] = $event_array;
}

$events = json_encode($events, JSON_PRETTY_PRINT);
print_r($events);
?>

Output