Get dates next week in PHP

I want echo dates from next week's Mo, Tu, We, Th, Fr, Sa, Su.

My code is as follows:

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

The problem is, for example, the sunday date is incorrect, because next Sunday is tomorrow, but I want a date from Sunday next week.

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

+6
source share
5 answers

This can be easily achieved using the 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('Ym-d'); 

Demo

+8
source

You can get the next week of 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'); 
+1
source

Say strtotime () that you are at the very beginning of β€œnext week” will already do this

 $next_week = strtotime('next week'); $date_monday = date("Ymd", strtotime('monday', $next_week)); $date_tuesday = date("Ymd", strtotime('tuesday', $next_week)); $date_wednesday = date("Ymd", strtotime('wednesday', $next_week)); $date_thursday = date("Ymd", strtotime('thursday', $next_week)); $date_friday = date("Ymd", strtotime('friday', $next_week)); $date_saturday = date("Ymd", strtotime('saturday', $next_week)); $date_sunday = date("Ymd", strtotime('sunday', $next_week)); 
0
source

Here is the function I created to get your needs:

 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('Ym-d', $date + $i * 86400); } return $dates; } 

Please note that the value of 86400 is the result of 24 * 60 * 60 , which means 86400 seconds or 1 day (24 hours * 60 minutes * 60 seconds).

To use it in your code, use the following:

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

There, I hope this helps!

0
source

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, it will help you

0
source

Source: https://habr.com/ru/post/958695/


All Articles