Get dates from next week in PHP
Asked Answered
M

6

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?

Measles answered 23/11, 2013 at 12:4 Comment(0)
L
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

Lamella answered 23/11, 2013 at 13:38 Comment(0)
C
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');
Circumspect answered 23/11, 2013 at 12:8 Comment(4)
and than $mon = $mon->format('d/m/Y'); and so on? but i just get the same date for every day (30/11/2013)...Measles
That won't work. $mon, $tue etc all reference the same object. You need to use DateTimeImmutable.Bronchiectasis
@Bronchiectasis thanks for your answer. is it possible that this thing does not exist in php 5.3?Measles
You can clone your objects, like this. This way will work on PHP >=5.2.0, where DateTimeImmutable only works on PHP >= 5.5.0.Extortionate
M
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));
Markusmarl answered 23/11, 2013 at 12:39 Comment(2)
next week on Sunday (2013-11-24) will return Monday (2013-12-02).Extortionate
@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) or even better, use my example. It doesn't only looks nicer, it is also more powerfull and faster, see simple benchmark.Extortionate
M
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!

Microcyte answered 24/11, 2013 at 1:18 Comment(0)
B
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);

try this this will help you

Beggary answered 16/7, 2016 at 6:31 Comment(1)
Try this is not an answer. Please explain how this solve the issue? Did you test out this answer yourself? (FYI: This code doesn't do anything)Pignut
B
-1

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>";
}
Beggary answered 17/6 at 5:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.