please, really need help with this problem.
I have array of offerDays – $offerDays = array(1,6);
(mean Monday, and Saturday);
For example, input date is Wednesday 2014/09/03. Next available date for offers is Saturday 2014/09/06
Question: How I can in php determine nearest next day? Something like
$offerDays = array(1,6); $inputDate = '2014/09/03'; // filled date, not actual date $offerDate = findDate($inputDate, $offerDays); // returns 2014/09/06 function findDate($inputDate, $offerDays) { return 'nearest next date to $inputDate defined by $offerDays'; }
Advertisement
Answer
Here you can have a variable array for the offerdays.
$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; }