从PHP获取下周的日期

时间:2013-11-23 12:04:44

标签: php date strtotime

我希望echo来自下周的Mo,Tu,We,Th,Fr,Sa,Su的日期。

我的代码目前看起来像这样:

$date_monday = date("Y-m-d", strtotime('next monday'));
$date_tuesday = date("Y-m-d", strtotime('next tuesday'));
$date_wednesday = date("Y-m-d", strtotime('next wednesday'));
$date_thursday = date("Y-m-d", strtotime('next thursday'));
$date_friday = date("Y-m-d", strtotime('next friday'));
$date_saturday = date("Y-m-d", strtotime('next saturday'));
$date_sunday = date("Y-m-d", strtotime('next sunday'));

问题是,例如sunday的日期是错误的,因为下一个星期日是明天,但我想要从下周的星期日开始的日期。

有没有办法将PHP日期设置为星期日并计算新日期的天数?

5 个答案:

答案 0 :(得分:8)

这可以通过DateTime类轻松实现:

$dt = new DateTime();
// create DateTime object with current time

$dt->setISODate($dt->format('o'), $dt->format('W') + 1);
// set object to Monday on next week

$periods = new DatePeriod($dt, new DateInterval('P1D'), 6);
// get all 1day periods from Monday to +6 days

$days = iterator_to_array($periods);
// convert DatePeriod object to array

print_r($days);
// $days[0] is Monday, ..., $days[6] is Sunday
// to format selected date do: $days[1]->format('Y-m-d');

<强> Demo

答案 1 :(得分:1)

你可以在下周星期天和......

$now = new DateTime();
    while ($now->format('D') != "Sun") {
    $now->modify("+1 day");
}

$mon = $now->format('d/m/Y');
$tue = $now->modify("+1 day")->format('d/m/Y');
$wed = $now->modify("+1 day")->format('d/m/Y');
$thu = $now->modify("+1 day")->format('d/m/Y');
$fri = $now->modify("+1 day")->format('d/m/Y');
$sat = $now->modify("+1 day")->format('d/m/Y');
$sun = $now->modify("+1 day")->format('d/m/Y');

答案 2 :(得分:0)

告诉strtotime()你正处于'下周'的开头已经会做到这一点

$next_week = strtotime('next week');
$date_monday = date("Y-m-d", strtotime('monday', $next_week));
$date_tuesday = date("Y-m-d", strtotime('tuesday', $next_week));
$date_wednesday = date("Y-m-d", strtotime('wednesday', $next_week));
$date_thursday = date("Y-m-d", strtotime('thursday', $next_week));
$date_friday = date("Y-m-d", strtotime('friday', $next_week));
$date_saturday = date("Y-m-d", strtotime('saturday', $next_week));
$date_sunday = date("Y-m-d", strtotime('sunday', $next_week));

答案 3 :(得分:0)

这是我为满足您的需求而创建的功能:

function datesOfNextWeek() {
  $dates = array();
  $date = time();                                 // get current date.
  while (date('w', $date += 86400) != 1);         // find the next Monday.
  for ($i = 0; $i < 7; $i++) {                    // get the 7 dates from it. 
    $dates[] = date('Y-m-d', $date + $i * 86400);
  }
  return $dates;
}

注意值8640024 * 60 * 60的结果,即86400秒或1天(24小时* 60分钟* 60秒)。

要在代码中使用它,请使用以下命令:

list(
  $date_monday, $date_tuesday, $date_wednesday,
  $date_thursday, $date_friday, $date_saturday,
  $date_sunday
) = datesOfNextWeek();

在那里,希望它有所帮助!

答案 4 :(得分:0)

function last_week_dates(){

$startdate = "last monday";
$day = strtotime($startdate);
$lastday = strtotime('sunday',$day);
$datareturn['1'][] = date('r',$day);
$datareturn['1'][] = date('r',$lastday);


$preday = strtotime('-7 days',$day);
$presund = strtotime('+6 days',$preday);
$datareturn['0'][] = date('r',$preday);
$datareturn['0'][] = date('r',$presund);


$futureday = strtotime('+7 days',$day);
$futuresund = strtotime('+6 days',$futureday);
$datareturn['0'][] = date('r',$futureday);
$datareturn['0'][] = date('r',$futuresund);
}

$ myweekdata = last_week_dates(); 的print_r($ myweekdata);

试试这个会帮到你