Get the date of next monday, tuesday, etc

前端 未结 10 1105
轻奢々
轻奢々 2020-11-29 02:24

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. Thank

相关标签:
10条回答
  • 2020-11-29 03:00

    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
    
    0 讨论(0)
  • 2020-11-29 03:02

    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'));
    
    0 讨论(0)
  • 2020-11-29 03:04

    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
    }
    
    0 讨论(0)
  • 2020-11-29 03:04

    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

    0 讨论(0)
  • 2020-11-29 03:04

    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
    
    0 讨论(0)
  • 2020-11-29 03:04

    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.

    0 讨论(0)
提交回复
热议问题