You can solve this problem with the following steps, generate a date range (the calendar of the year itself) with the help of the classes DateInterval
and DatePeriod
defining the increment unit (PD1) of the range.
Then take the current day of the foreach and compare with the list of exceptions and do that in_array()
skip them, this will generate the SQL string with multiple values to run only an Insert at the end.
IS important comment that the date range does not include the final date in case 31/12/2018 so the line: $dataFim->modify('+1 day');
. modify()
adds one more day to the date it turns 01/01/2019
.
$dataInicio = new DateTime('2018-01-01');
$dataFim = new DateTime('2018-12-31');
$dataFim->modify('+1 day');
$intervalo = new DateInterval('P1D');
$diasAno = new DatePeriod($dataInicio, $intervalo, $dataFim);
$selecionados = '01/01,02/01,04/01,12/01';
$excecoes = explode(',', $selecionados);
foreach($diasAno as $date){
if(!in_array($date->format('d/m'), $excecoes)){
$sql .= sprintf("('%s'),", $date->format('Y-m-d'));
}
}
$sql = rtrim($sql, ',');
echo $sql;
The exit is something like:
('2018-01-01'),('2018-01-02'),('2018-01-03'),('2018-01-04')