What I am trying to do is get the date ($date_before_that_expiry) which is $time_to_expiry (6) days before $distinct_expiries (26-Oct-2017) in the format DD-MM-YYYY
However, if the returned date is is in array ($trading_holiday_array) then it should move the returned date ($date_before_that_expiry) one day before. This is to be done recursively.
Eg: if I get a date 20-Oct-2017 initially it should return me 18-Oct-2017 and not 19th as 19-Oct-2017 is also a part of ($trading_holiday_array).
Here is the code I have put down but it goes into an infinite loop:
<?php
$distinct_expiries='26-Oct-2017';
$time_to_expiry=6;
$trading_holiday_array = array('19-Oct-2017', '20-Oct-2017');
$date_before_that_expiry = date('d-M-Y', strtotime("-$time_to_expiry days", strtotime($distinct_expiries)));
while(in_array($date_before_that_expiry, $trading_holiday_array)) {
$new_lookback= -(1+$time_to_expiry)." days";
$date_before_that_expiry = date('d-M-Y', strtotime("$new_lookback", strtotime($distinct_expiries)));
}
echo $date_before_that_expiry;
?>
Please assist.
DateTimeobjects. Always useDateTimeobjects internally; never use string format dates anywhere in your code except for the edges (ie the point of input, and when you display them back to the screen). Once you have your dates stored as objects, you'll be able to search, sort, etc much more easily.ifinstead ofwhileand infinite loop solved.