简体   繁体   中英

Getting the previous and next 7 days from a specific date in PHP?

Getting the previous and next 7 days from a specific date

$date = new DateTime();
$today = $date->format('m d, Y');

How can I get the previous 7 and next 7 days from $today? and loop them in an array.

Something different to the other answers but what about

$start   = new DateTime();
$end     = new DateTime();

$start   = $start->modify( '-7 days' ); 
$end     = $end->modify( '+8 days' ); // Date Period doesn't include the end date

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($start, $interval ,$end);

foreach($daterange as $date){
    echo $date->format("'m d, Y'") . "<br>";
}

可能最适合这种情况:

DateTime::modify

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