Skip to content

Instantly share code, notes, and snippets.

@brendomaciel
Last active September 22, 2016 03:15
Show Gist options
  • Save brendomaciel/360062cd146d67da69d60bb1e9700dbf to your computer and use it in GitHub Desktop.
Save brendomaciel/360062cd146d67da69d60bb1e9700dbf to your computer and use it in GitHub Desktop.
Função para retornar um conjunto de datas de dias específicos $days — a partir da data atual — com tamanho $limit.
<?php
function get_week_by_date($date) {
$date = strtotime($date);
$week_position = date('w', $date);
// Seeking sunday
$sunday = ($week_position == 0) ? $date : strtotime('last sunday', $date);
$week = [];
// Adding sunday
$week[] = date('Y-m-d', $sunday);
// Adding other days based on sunday
for ($i = 1; $i <= 6; $i++) {
$day = date('Y-m-d', strtotime("+{$i} day", $sunday));
$week_position = date('w', $day);
$week[$week_position] = $day;
}
return $week;
}
function next_days($days, $limit) {
$i = 0;
$last_week = date('Y-m-d');
$result = [];
while ($i < $w) {
$week = get_week_by_day($last_week);
foreach ($days as $day) {
if ($i < $limit) {
$result[] = $week[$day];
$i++;
} else {
break;
}
}
$last_week = date('Y-m-d', strtotime('+1 week', $last_week));
if ($limit >= $i) {
break;
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment