Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Dates of the Last 7 Days in Array In Custom Format

Tags:

php

datetime

How do I get the exact dates of the last 7 days including today in a custom format (dd/mm)?

In the resulting array I would like to get something like (dates are examples only):

1=>11/2 (today minus 7 days)
2=>12/2 (today minus 6 days)
...
7=>17/2 (today)
like image 795
Dzhuneyt Avatar asked Nov 28 '22 06:11

Dzhuneyt


1 Answers

function getLastNDays($days, $format = 'd/m'){
    $m = date("m"); $de= date("d"); $y= date("Y");
    $dateArray = array();
    for($i=0; $i<=$days-1; $i++){
        $dateArray[] = '"' . date($format, mktime(0,0,0,$m,($de-$i),$y)) . '"'; 
    }
    return array_reverse($dateArray);
}

Usage:

$arr = getLastNDays(7);

or

$arr = getLastNDays(7, 'd/m/Y');
like image 175
Dzhuneyt Avatar answered Dec 18 '22 11:12

Dzhuneyt