How to create an array of days starting with today and going back the last 30 days in PHP

1 Answer

0 votes
$days = array();

for ($i = 0; $i < 30; $i++) {
    $days[] = date("d", strtotime('-'. $i .' days'));
}

print_r($days);



/*
run:

Array
(
    [0] => 10
    [1] => 09
    [2] => 08
    [3] => 07
    [4] => 06
    [5] => 05
    [6] => 04
    [7] => 03
    [8] => 02
    [9] => 01
    [10] => 31
    [11] => 30
    [12] => 29
    [13] => 28
    [14] => 27
    [15] => 26
    [16] => 25
    [17] => 24
    [18] => 23
    [19] => 22
    [20] => 21
    [21] => 20
    [22] => 19
    [23] => 18
    [24] => 17
    [25] => 16
    [26] => 15
    [27] => 14
    [28] => 13
    [29] => 12
)

*/

 



answered Apr 10, 2025 by avibootz
edited Apr 10, 2025 by avibootz
...