Get the date of next monday, tuesday, etc

PhpDatetime

Php Problem Overview


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!

Php Solutions


Solution 1 - Php

See strtotime()

strtotime('next tuesday');

You could probably find out if you have gone past that day by looking at the week number:

$nextTuesday = strtotime('next tuesday');
$weekNo = date('W');
$weekNoNextTuesday = date('W', $nextTuesday);

if ($weekNoNextTuesday != $weekNo) {
    //past tuesday
}

Solution 2 - Php

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

Solution 3 - Php

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'));

Solution 4 - Php

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

Solution 5 - Php

You can use Carbon library.

Example: Next week friday

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

Solution 6 - Php

PHP 7.1:

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

PHP manual getTimestamp()

Solution 7 - Php

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

Solution 8 - Php

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.

Solution 9 - Php

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

Solution 10 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdrian SarliView Question on Stackoverflow
Solution 1 - PhpTom HaighView Answer on Stackoverflow
Solution 2 - Phpuser4446130View Answer on Stackoverflow
Solution 3 - PhpJosh DavisView Answer on Stackoverflow
Solution 4 - PhpDenimar FernandezView Answer on Stackoverflow
Solution 5 - PhpFarid MovsumovView Answer on Stackoverflow
Solution 6 - PhpOksana RomanivView Answer on Stackoverflow
Solution 7 - PhpLarry WatanabeView Answer on Stackoverflow
Solution 8 - PhpMikeView Answer on Stackoverflow
Solution 9 - PhpEannView Answer on Stackoverflow
Solution 10 - PhpDexygenView Answer on Stackoverflow