[php] Get the date of next monday, tuesday, etc

I would like to find the date stamp of monday, tuesday, wednesday, etc. If that day hasn't come this week yet, I would like the date to be this week, else, next week. Thanks!

This question is related to php datetime

The answer is


The PHP documentation for time() shows an example of how you can get a date one week out. You can modify this to instead go into a loop that iterates a maximum of 7 times, get the timestamp each time, get the corresponding date, and from that get the day of the week.


You can use Carbon library.

Example: Next week friday

Carbon::parse("friday next week");

Sorry, I didn't notice the PHP tag - however someone else might be interested in a VB solution:

Module Module1

    Sub Main()
        Dim d As Date = Now
        Dim nextFriday As Date = DateAdd(DateInterval.Weekday, DayOfWeek.Friday - d.DayOfWeek(), Now)
        Console.WriteLine("next friday is " & nextFriday)
        Console.ReadLine()
    End Sub

End Module

PHP 7.1:

$next_date = new DateTime('next Thursday');
$stamp = $next_date->getTimestamp();

PHP manual getTimestamp()


I know it's a bit of a late answer but I would like to add my answer for future references.

// Create a new DateTime object
$date = new DateTime();

// Modify the date it contains
$date->modify('next monday');

// Output
echo $date->format('Y-m-d');

The nice thing is that you can also do this with dates other than today:

// Create a new DateTime object
$date = new DateTime('2006-05-20');

// Modify the date it contains
$date->modify('next monday');

// Output
echo $date->format('Y-m-d');

To make the range:

$monday = new DateTime('monday');

// clone start date
$endDate = clone $monday;

// Add 7 days to start date
$endDate->modify('+7 days');

// Increase with an interval of one day
$dateInterval = new DateInterval('P1D');

$dateRange = new DatePeriod($monday, $dateInterval, $endDate);

foreach ($dateRange as $day) {
    echo $day->format('Y-m-d')."<br />";
}

References

PHP Manual - DateTime

PHP Manual - DateInterval

PHP Manual - DatePeriod

PHP Manual - clone


For some reason, strtotime('next friday') display the Friday date of the current week. Try this instead:

//Current date 2020-02-03
$fridayNextWeek = date('Y-m-d', strtotime('friday next week'); //Outputs 2020-02-14

$nextFriday = date('Y-m-d', strtotime('next friday'); //Outputs 2020-02-07

If I understand you correctly, you want the dates of the next 7 days?

You could do the following:

for ($i = 0; $i < 7; $i++)  
  echo date('d/m/y', time() + 86400 * $i);

Check the documentation for the date function for the format you want it in.


if you want to find Monday then 'dayOfWeek' is 1 if it is Tuesday it will be 2 and so on.

var date=new Date();
getNextDayOfWeek(date, 2);

// this is for finding next tuesday

function getNextDayOfWeek(date, dayOfWeek) {
// Code to check that date and dayOfWeek are valid left as an exercise ;)

var resultDate = new Date(date.getTime());

resultDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);

return resultDate;
}

Hope this will be helpfull to you, thank you


The question is tagged "php" so as Tom said, the way to do that would look like this:

date('Y-m-d', strtotime('next tuesday'));