• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1077

# Get date of last 30 days

Hi,

I'm using the following function to output dates for a period of time (days).
However, it does output the dates in the wrong order, from newest to oldest.

How do I reverse the order (ex. 28th, 29th, 30th, 1st, 2nd... etc).

``````function createDatesArray(\$days) {

#CLEAR OUTPUT FOR USE
\$output = array();

#SET CURRENT DATE
\$month = date("m");
\$day = date("d");
\$year = date("Y");

#LOOP THROUGH DAYS
for(\$i=1; \$i<=\$days; \$i++){
\$output[] = strtotime(date('Y-m-d',mktime(0,0,0,\$month,(\$day-\$i),\$year)));
}

#RETURN DATE ARRAY
return \$output;

}
``````

Output
``````<?php
# BUILD ARRAY OF DATES USING FUNCTION
\$dates = createDatesArray("30");

# OUTPUT A LIST OF THE LAST 30 DAYS IN ORDER
foreach(\$dates as \$date) {

echo '<th scope="col">'.date("jS\nD",\$date).'</th>';
}
?>
``````
0
kgp43
1 Solution

Commented:
#LOOP THROUGH DAYS
for(\$i=\$days; \$i>=1; \$i--){
\$output[] = strtotime(date('Y-m-d',mktime(0,0,0,\$month,(\$day-\$i),\$year)));
}
0

Author Commented:
Perfect, thanks a lot :)
0

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.