3
votes

I hope that someone can help me out. I am trying to build a dropdown menu, which shows the next 4-6 tuesdays with the correct date. This is already working. Problem now is, that I need the following:

If today is wednesday, the dropdown should start with the first tuesday BUT not from next week, instead with the tuesday from the week after next week. Why? Because I always need full 7 days between the registration and the delivery time.

So if today is monday, the next (first) tuesday should be the tuesday from next week. Also lets say today is friday, then my dropdown should not start with the tuesday from next week, instead with the tuesday from the week after next week.

Something like: Today is Monday - tomorrow will be tuesday but there are not 7 days between so the tuesday from next will should be shown.

Here is what I have so far:

$begin = new DateTime('tuesday this week');
$end = new DateTime('last tuesday of next month');

$interval = new DateInterval( 'P1W' );
$daterange = new DatePeriod( $begin, $interval ,$end );

<select class='form-control margin-bottom-20' name='delivery_first' required>
    foreach($daterange as $date){
        <option value='".$date->format('d.m.Y')."'>"; echo $date->format('l'); echo $date->format('d.m.Y'); print"</option>
    }
</select>

Any idea how I can implement the check, if 7 days are between? Hope my question is clear. If not please let me know.

5
What if it's Tuesday 23:59? Next week or two weeks ahead?Andreas
When would the next Tuesday ever be less than 7 days from the current week? If it's Wednesday today, the next Tuesday is in six days. Even if it's Tuesday, the next Tuesday is still <= 7 days away. Why not just always pull the Tuesday of next week?Winch

5 Answers

3
votes

You could use a minimum datetime instance to compare against.

$begin = new DateTime('next tuesday');
$min = new DateTime('+7 days');
$interval = new DateInterval( 'P1W' );

if ($begin < $min) {
    $begin->add($interval);
}

However, if you always want two tuesdays from now, this simple change might work:

$begin = new DateTime('next tuesday +7 days');
1
votes

You can use strtotime() to get a unix timestamp (value in seconds) of your date, and use that to compare the lengths of time between two dates.

$now = strtotime('now');
$begin = new DateTime('tuesday this week');

$seconds_between_now_and_tuesday = (strtotime($begin) - strtotime($now));
$days_between_now_and_tuesday = $seconds_between_now_and_tuesday / 86400; //86,400 seconds in a day
if($days_between_now_and_tuesday < 7) {
    $begin = new DateTime('tuesday next week');
}

http://php.net/manual/en/function.strtotime.php

1
votes

This is just pseudo-code, so don't just copy and paste. I think you're needing a switch statement for your $begin date.

switch($todayDay){

    case Wednesday, Thursday, Friday:
        $begin = new DateTime('tuesday 2 weeks');
        break;

    case Monday, Tueday:
        $begin = new DateTime('tuesday this week');
        break;
}
1
votes

You can also try this.

// create date object
$d2 = new DateTime();  

//Add 6 days to the object, should be atleast six days in between 
$d2->add(new DateInterval("P6D")); 

//  Start counting from after 6 days, take the first 
//  tuesday after 6 days and print the date and break out of the loop

for($i=1; $i<7; $i++){  
 $j="P1D";
 $d2->add(new DateInterval($j));
 $date2 = date('l',$d2->getTimeStamp());  
  if(strtolower($date2) == 'tuesday'){

      echo $d2->format('Y-m-d H:i:s');
     break;   
  }
}
1
votes

Strtotime can read datetimes in a YWwwd code for example 2018W242 which translates to 2018 week 24 day 2.

With that in mind we can just look at what day of the week it is then add to the week start variable 1 or 2.
Then just loop and echo according to the format above mentioned.
Usually strtotime and date is lighter on memory and run times than datetime.

If(date("N")>1){
   $w = date("W")+2;
}Else{
   $w =date("W")+1;
}

$y = date("Y");
$d = date("Y\WWN", strtotime($y . "W" . $w . "2"));

For($i=1; $i<7;$i++){
    Echo date("Y-m-d", strtotime($d)+(86400*7)*$i) ."\n";
}

https://3v4l.org/SMaou

Edited code, noticed it didn't work past new year