7

I want to echo the dates from Mo,Tu,We,Th,Fr,Sa,Su of the next week.

My code looks like this at the moment:

$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'));

Problem is that for example the date of sunday is wrong, because the next sunday is tomorrow, but I want the date from the sunday of the following week.

Is there a way to set the PHP date to sunday and calculate the days with the new date?

John Brunner
  • 2,842
  • 11
  • 44
  • 85

5 Answers5

10

This can easily be achieved via DateTime class:

$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

Glavić
  • 42,781
  • 13
  • 77
  • 107
2

You can get the next week sunday and ...

$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');
sensorario
  • 20,262
  • 30
  • 97
  • 159
  • and than `$mon = $mon->format('d/m/Y');` and so on? but i just get the same date for every day (30/11/2013)... – John Brunner Nov 23 '13 at 12:19
  • 3
    That won't work. $mon, $tue etc all reference the same object. You need to use DateTimeImmutable. – vascowhite Nov 23 '13 at 12:30
  • @vascowhite thanks for your answer. is it possible that this thing does not exist in php 5.3? – John Brunner Nov 23 '13 at 12:58
  • You can [clone](http://php.net/manual/en/language.oop5.cloning.php) your objects, like [this](https://eval.in/72770). This way will work on PHP >=5.2.0, where `DateTimeImmutable` only works on PHP >= 5.5.0. – Glavić Nov 23 '13 at 13:45
1

Telling the strtotime() you are at the very start of 'next week' already will do this

$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));
StigM
  • 711
  • 6
  • 12
  • `next week` on Sunday (2013-11-24) will return Monday (2013-12-02). – Glavić Nov 23 '13 at 14:06
  • @JohnBrunner: don't take my word for it, but I think that because PHP by default uses Gregorian calendar for this kind of calculations; and Gregorian weeks start on Sunday and not Monday, like ISO-8601 standard. To fix that, you can set `$next_week` like `$next_week = strtotime('+7 day', strtotime(date('o-\WW-1')));` ([demo](https://eval.in/72785)) or even better, use my [example](http://stackoverflow.com/a/20162989/67332). It doesn't only looks nicer, it is also more powerfull and faster, see simple [benchmark](https://eval.in/72788). – Glavić Nov 23 '13 at 15:05
1

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);

try this this will help you

roshan
  • 11
  • 2
0

Here's the function I created to get your need:

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;
}

Note the value 86400 is the result of 24 * 60 * 60, which means the 86400 seconds or 1 days (24 hours * 60 minutes * 60 seconds).

To use it on your code, use the following:

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

There, hope it helps!

Choerun Asnawi
  • 181
  • 1
  • 5