douchensou6969
2013-01-07 22:37
浏览 393
已采纳

从PHP中的特定日期开始上一天和后7天?

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.

图片转代码服务由CSDN问答提供 功能建议

获取特定日期的前7天和后7天

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

我如何获得 从今天开始的7天和未来7天? 并将它们循环到一个数组中。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douquanjie9326 2013-01-07 23:30
    已采纳

    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>";
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题