简体   繁体   中英

How can I get every date (UTC) from the last 30 days in PHP?

How can I get an array with all the dates in UTC (YYYY-MM-DD) from the last 30 days?` I tried to make a loop / array but I didnt succeed and only got every day in the last 30 days but the month/year didnt change..

Does someone have an idea? :)) Thanks alot!

<?php    
$d = array();
for($i = 0; $i < 30; $i++) 
$d[] = date("d", strtotime('-'. $i .' days'));
?>

You can do date time modify minus one day.

$dates = array();
$DateTime = new DateTime();
for($i = 0; $i < 30; $i++) {
    $DateTime->modify( "-1 days" );
    $dates[] =  $DateTime->format('Y-m-d');
}

Each iteration you subtract one day, and then save the formatted date, and repeat.

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM