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

1 Answer

0 votes
$dates = [];
$today = new DateTime();

for ($i = 0; $i < 30; $i++) {
    $dates[] = $today->format('Y-m-d');
    $today->modify('-1 day');
}

print_r($dates);


/*
run:

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

*/

 



answered Apr 10 by avibootz
...