I was wondering if somebody could help with this problem I have.
I have an array which contains the weekdays in which people want to be contacted. I have then created a for loop which adds 1 day to the current date until the day of the week matches a day in which a person wants to be contacted.
The problem I am having is that the loop always goes one day too far. I am just wondering if this is the best way to accomplish what I am trying to do or is there a better way?
Here is my code:
$ScheduleWindow = array('Monday', 'Tuesday');
$date = new DateTime('today');
$dow = getdate($date->getTimestamp());
for($date, $dow;!in_array($dow['weekday'], $ScheduleWindow);$date->add(new DateInterval('P1D'))){
$dow = getdate($date->getTimestamp());
}
echo "Next date to contact is" . $date->format('Y-m-d H:i:s') . "\n";
The code currently echoes “Next date in schedule window is2011-01-25 00:00:00” however I need it to be the date 2011-01-24.
Thanks for looking.
Your
forloop expressed in words does this:You’re checking against the un-increased date on the next loop.
Your loop is also much too complicated. This should do just fine:
Your
DateTimeobject,getdate,DateIntervaloperation is much more complex than it needs to be: