Get dates from next week in PHP
10,424
Solution 1
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');
Solution 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');
Solution 3
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));
Solution 4
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
Author by
John Brunner
Updated on June 22, 2022Comments
-
John Brunner almost 2 years
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?