tags:

views:

1960

answers:

6

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!

+10  A: 

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
}
Tom Haigh
Thanks! This helps a lot. I just have to find out how to find out if monday has already come. That won't be too hard! Thanks!
Adrian Sarli
A: 

Not sure exactly what you are asking.... but if you have a DayOfTheWeek function it is a trivial task. Here is the SQL that selects this Thursday unless it is already Wednesday, then it selects 2 Thursdays from now.

declare @dw int
declare @retDate datetime

set @dw = datepart(dw,getdate())

if @dw < 4 
    set @retDate = dateadd(dd,5-@dw,getdate())
else
    set @retDate = dateadd(dd,12-@dw,getdate())


select cast(convert(varchar(40),@retDate,101) as DateTime)
JBrooks
+2  A: 

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.

Mike
A: 

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.

George Jempty
+1  A: 

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'));
Josh Davis
A: 

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

Larry Watanabe