Get the date of next Monday, Tuesday, etc.

I would like to find the date stamp on Monday, Tuesday, Wednesday, etc. If this day has not yet arrived this week, I would like the date to be this week, otherwise, next week. Thank!

+49
php datetime
Jul 27 '09 at 15:09
source share
7 answers

See strtotime()

 strtotime('next tuesday'); 

Perhaps you found out if you passed this day by looking at the week number:

 $nextTuesday = strtotime('next tuesday'); $weekNo = date('W'); $weekNoNextTuesday = date('W', $nextTuesday); if ($weekNoNextTuesday != $weekNo) { //past tuesday } 
+90
Jul 27 '09 at 15:15
source share

I know this is a bit late answer, but I would like to add my answer for future links.

 // Create a new DateTime object $date = new DateTime(); // Modify the date it contains $date->modify('next monday'); // Output echo $date->format('Ym-d'); 

It's nice 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('Ym-d'); 

To make a 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('Ym-d')."<br />"; } 

References

PHP Guide - DateTime

PHP Guide - DateInterval

PHP Guide - DatePeriod

PHP manual - clone

+28
Jan 28 '15 at 15:10
source share

The question is marked as "php", as Tom said, a way to do this would look like this:

 date('Ym-d', strtotime('next tuesday')); 
+20
Jul 27 '09 at 15:18
source share

Sorry, I didn’t notice the PHP tag, however someone might be interested in solving VB:

 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 
+4
Jul 27 '09 at 15:46
source share

If I understand you correctly, do you need dates for the next 7 days?

You can do the following:

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

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

+2
Jul 27 '09 at 15:16
source share

if you want to find Monday, then "dayOfWeek" is 1, if on Tuesday it will be 2, etc.

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

// this is to search for the 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 it will be useful for you, thanks

+1
Nov 20 '14 at 11:39
source share

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

0
Jul 27 '09 at 15:17
source share



All Articles