Question:
I need to create a date list in PHP where I will list all the dates from the first day of the year to the last. However, in this list, dates referring to the weekend (Saturday and Sunday) should be skipped.
Example:
01/02/2016 => Segunda
02/02/2016 => Terça
03/02/2016 => Quarta
04/02/2016 => Quinta
05/02/2016 => Sexta
08/02/2016 => Segunda
Does anyone have any idea how to do this in php?
Answer:
You can use DatePeriod class to set up the desired time period, in the example I used the month of February, to know which days are weekends (Saturday and Sunday) English helps, because both days start with S(saturday and sunday ), it is enough to know if the weekday starts with S or not.
$inicio = new DateTime('2016-02-01');
$fim = new DateTime('2016-02-29');
$periodo = new DatePeriod($inicio, new DateInterval('P1D'), $fim);
$validos = [];
foreach($periodo as $item){
if(substr($item->format("D"), 0, 1) != 'S'){
$validos[] = $item->format('d/m/Y');
}
}
echo "<pre>";
print_r($validos);
Sáida:
Array
(
[0] => 01/02/2016
[1] => 02/02/2016
[2] => 03/02/2016
[3] => 04/02/2016
[4] => 05/02/2016
[5] => 08/02/2016
[6] => 09/02/2016
[7] => 10/02/2016
[8] => 11/02/2016
[9] => 12/02/2016
[10] => 15/02/2016
[11] => 16/02/2016
[12] => 17/02/2016
[13] => 18/02/2016
[14] => 19/02/2016
[15] => 22/02/2016
[16] => 23/02/2016
[17] => 24/02/2016
[18] => 25/02/2016
[19] => 26/02/2016
)