John Brunner
John Brunner

Reputation: 2872

Get dates from next week in PHP

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?

Upvotes: 7

Views: 14248

Answers (6)

roshan
roshan

Reputation: 7

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

Upvotes: 0

roshan
roshan

Reputation: 7

please find code for php alredy gave you next monday from current week and then start a loop of seven day from monday

$next_monday_timestamp = strtotime('next monday');
for ($i = 0; $i < 7; $i++) {
$timestamp = strtotime("+$i days",$next_monday_timestamp);
    $date = date('Y-m-d', $timestamp);
    echo date('l', $timestamp) . ": " . $date . "<br>";
}

Upvotes: -1

Choerun Asnawi
Choerun Asnawi

Reputation: 181

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!

Upvotes: 0

sensorario
sensorario

Reputation: 21600

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

Upvotes: 2

Glavić
Glavić

Reputation: 43552

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

Upvotes: 10

StigM
StigM

Reputation: 711

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

Upvotes: 1

Related Questions