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?
echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>"); echo(strtotime("next Monday") . "<br>");
$d=strtotime("tomorrow");
Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date.
The strtotime() function is a built-in function in PHP which is used to convert an English textual date-time description to a UNIX timestamp. The function accepts a string parameter in English which represents the description of date-time. For e.g., “now” refers to the current date in English date-time description.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With