PHP -> Next nearest date defined by array of days in week



PHP Snippet 1:

$offerDays = array(1,6);
$inputDate = '2014/09/04'; // filled date, not actual date
$offerDate = findDate($inputDate, $offerDays);

function findDate($inputDate, $offerDays) {
   $date = DateTime::createFromFormat('Y/m/d', $inputDate);
   $num = $date->format('w');

   $min = 10; //initialize minimum days
   foreach($offerDays as $o){  //loop through all the offerdays to find the minimum difference
     $dif = $o - $num;
     if($dif>0 && $dif < $min){
        $min = $dif ;
       }
   }
   // if minimum days is not changed then get the first offerday from next week
   if($min == 10){
      $min = 6 - $num + min($offerDays);
   }

   //add the days till next offerday
   $add = new DateInterval('P'.$min.'D');
   $nextOfferDay = $date->add($add)->format('Y/m/d');

  return $nextOfferDay;
}

PHP Snippet 2:

$currentDate = date('Y-m-d');
$frequencyDays = [2, 4, 5];

function nextNearestDate($currentDate, $frequencyDays) {
    $weekDay = date('w', strtotime($currentDate));
    $min = 10; // Some random number>7
    foreach ($frequencyDays as $day) {
        $dif = $day - $weekDay;
        if ($dif == 0) {
            $min = $dif;
            break;
        }else if ($dif > 0) {
            $positiveDay[] = $dif;
        }else if ($dif < 0) {
            $negativeDay[] = $dif;
        }
    }
    if ($min != 0) {
        if (isset($positiveDay) && count($positiveDay)) {
            $min = min($positiveDay);
        } else {
            $min = min($negativeDay);
            $min = 7 + $min;
        }
    }
    return $newDay = date('Y-m-d', strtotime('+' . $min . ' days', strtotime($currentDate)));
}

PHP Snippet 3:

 $date_str="2014-09-03";
 if(date('l' , $date_str) != 'Saturday')
 {
     //Your Code Here

 }

PHP Snippet 4:

<?php

/**
 * Return the next date that falls on one of the specified weekdays.
 *
 * @param  DateTimeInterface $inputDate The search begins on the day after this
 * @param  array             $weekdays  An array of permitted weekdays
 *                                        represented by an integer (0=Sunday)
 *
 * @return DateTimeInterface|null  The first date that meets the criteria
 */
function findDate(DateTimeInterface $inputDate, array $weekdays)
{
    $weekdays = array_flip($weekdays);

    foreach (new DatePeriod($inputDate, new DateInterval('P1D'), 7, DatePeriod::EXCLUDE_START_DATE) as $possibleDate)
    {
        if (isset($weekdays[$possibleDate->format('w')]) === true)
        {
            return $possibleDate;
        }
    }
}

// Example:

$weekdays = [1, 6];
$inputDate = '2014/09/03';

$offerDate = findDate(new DateTimeImmutable($inputDate), $weekdays);

echo $offerDate->format('Y-m-d'), "\n";

PHP Snippet 5:

<?php

function WeekDayIterator(DateTimeInterface $inputDate, array $weekdays)
{
    $weekdays = array_intersect($weekdays, range(0,6));

    if (!$weekdays)
    {
        return;
    }

    $possibleDate = new DateTimeImmutable('@' . $inputDate->getTimeStamp());
    $oneDay = new DateInterval('P1D');
    $weekdays = array_flip($weekdays);

    while (true)
    {
        $possibleDate = $possibleDate->add($oneDay);
        if (isset($weekdays[$possibleDate->format('w')]) === true)
        {
            yield $possibleDate;
        }
    }
}

function findDate(DateTimeInterface $inputDate, array $weekdays)
{
    return WeekDayIterator($inputDate, $weekdays)->current();
}

// Example:

$weekdays = [1, 6];
$inputDate = '2014/09/03';

$offerDate = findDate(new DateTimeImmutable($inputDate), $weekdays);

echo $offerDate->format('Y-m-d'), "\n";

foreach (new LimitIterator(WeekDayIterator(new DateTimeImmutable($inputDate), $weekdays), 0, 10) as $date)
{
    echo $date->format('Y-m-d (w)'), "\n";
}