1

So I have a date input that automatically sets the date 8 days later like this:

<input type="date" name="pdate" value="<?php echo date("Y-m-d", time() + 691200); ?>" required>

So now this will display 03/13/2021 as the date, but is it possible to skip Saturday and Sunday, so if the date in 8 days is Saturday or Sunday, it will automatically set date as the Money after insted?

Carben
  • 41
  • 1
  • 7
  • I would advise you to create either a javascript or php function that will be called and checks if it's a saturday or sunday and sets the date to the next monday if thats true – Keimeno Mar 05 '21 at 14:47
  • 2
    See [this question](https://stackoverflow.com/questions/40764671/how-to-get-date-5-to-exclude-weekends/). There are multiple methods, but the best one is (at this moment) hidden in a [comment](https://stackoverflow.com/questions/40764671/how-to-get-date-5-to-exclude-weekends/#comment68754368_40765115). – El_Vanja Mar 05 '21 at 14:53
  • so first, is the answer of 3/13/2021 correct, or is your intention to have the output be 3/17/2021 .. 8 weekdays in the future? second, you are concerned about the day that 8 days in the future lands on so 3/13/2021 would need to be shifted to 3/15/2021 – Ryan H Mar 05 '21 at 15:03
  • @RyanH I want the value in date input to display date 8 days in the future, BUT - if the 8. day in the future is a Saturday or Sunday, I want it to automatically select the Monday after instead. If the 8 day in the future is a weekday, for example Wednesday, it should stay like that. – Carben Mar 05 '21 at 15:07
  • Have you tried searching for the functionalities you need? Like [checking if a date is weekend](https://stackoverflow.com/questions/4802335/checking-if-date-is-weekend-php)? And with [relative formats](https://www.php.net/manual/en/datetime.formats.relative.php), next monday is trivial to fetch. – El_Vanja Mar 05 '21 at 15:12

2 Answers2

0

Yes, you can use DateTime in PHP

Like below

new \DateTime('+8 weekdays');
dev_mustafa
  • 1,042
  • 1
  • 4
  • 14
0
<?php

$date = new \DateTime('+8 days');
$date = shiftDate($date);
function shiftDate($date){
    if (in_array((int)$date->format('N'), [6,7])){
        $date->add(new \DateInterval('P1D'));
        $date = shiftDate($date);
    }
    return $date;
}
var_dump($date);

working code here https://3v4l.org/vmomV

Ryan H
  • 400
  • 2
  • 9
  • Nice! But why doesn't `value=""` give me the date value in the date input? – Carben Mar 05 '21 at 15:14
  • sorry you would need to use `$date->format("Y-m-d")` – Ryan H Mar 05 '21 at 15:15
  • docs on the DateTime object https://www.php.net/manual/en/class.datetime.php – Ryan H Mar 05 '21 at 15:16
  • Instead of calling your function recursively, you could simply do `->modify('next monday')` if the date is on a weekend. – El_Vanja Mar 05 '21 at 15:18
  • @El_Vanja true, I have a more complex version of this that also skips bank holidays and company holidays so in my original version recursion was more appropriate as it could shift to any day of the week in theory. Glad it worked for you – Ryan H Mar 05 '21 at 15:24