Answer the question
In order to leave comments, you need to log in
How to get the dates of the last five days?
For example, today is Aug 15 How
to get an array of the last five
days ? Aug
You can link to some article or help with the code.
Answer the question
In order to leave comments, you need to log in
Example using UnixTimeStamp:
function getLastNDays($days_count = 5) {
$today = time();
$last_days = [];
for ($i = 0; $i < $days_count; $i++) {
$prev_day = $today - (3600 * 24 * $i);
array_push($last_days, $prev_day);
}
return $last_days;
}
// Вывод
$days = array_map(function($item){
return date('d M', $item);
}, getLastNDays(5));
asort($days);
echo implode(', ', $days); // 11 Aug, 12 Aug, 13 Aug, 14 Aug, 15 Aug
function getLastNDays($days_count = 5) {
$datetime = new DateTime();
$last_days = [];
for ($i = 0; $i < $days_count; $i++) {
array_push($last_days, clone $datetime);
$datetime->sub(new DateInterval('P1D'));
}
return $last_days;
}
// Вывод
$days = array_map(function($item){
return $item->format('d M');
}, getLastNDays(5));
asort($days);
echo implode(', ', $days); // 11 Aug, 12 Aug, 13 Aug, 14 Aug, 15 Aug
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question