Home > Enterprise >  Sort data display last 30 days data with foreach PHP
Sort data display last 30 days data with foreach PHP

Time:10-13

i will sort data display last 30 days data with foreach with PHP.

This my code:

for($i = 0; $i < 30; $i  ) {
       echo  date("Y-m-d", strtotime('-'. $i .' days')).'<br>';
   }

this is the result:

2021-10-07
2021-10-06
2021-10-05
2021-10-04
2021-10-03
2021-10-02
2021-10-01
2021-09-30
2021-09-29
2021-09-28
2021-09-27
2021-09-26
2021-09-25
2021-09-24
2021-09-23
2021-09-22
2021-09-21
2021-09-20
2021-09-19
2021-09-18
2021-09-17
2021-09-16
2021-09-15
2021-09-14
2021-09-13
2021-09-12
2021-09-11
2021-09-10
2021-09-09
2021-09-08

Then I want to sort the data from the oldest date to the new date. From 2021-09-08 to 2021-10-07.

how to do it, is there any other solution?

CodePudding user response:

Check this out

for($i = 30; $i >= 0; $i--) {
       echo  date("Y-m-d", strtotime('-'. $i .' days')).'<br>';
}

  • Related