Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3785672
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:33:52+00:00 2026-05-19T11:33:52+00:00

I was wondering if somebody could help with this problem I have. I have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T11:33:53+00:00Added an answer on May 19, 2026 at 11:33 am

    Your for loop expressed in words does this:

    If the weekday in $dow is not within schedule window,  
    assign the current date to $dow,
    increase the date,
    repeat.
    

    You’re checking against the un-increased date on the next loop.

    Your loop is also much too complicated. This should do just fine:

    while (!in_array($dow['weekday'], $ScheduleWindow)) {
        $date->add(new DateInterval('P1D'));
        $dow = getdate($date->getTimestamp());
    }
    

    Your DateTime object, getdate, DateInterval operation is much more complex than it needs to be:

    $scheduleWindow = array('Monday', 'Tuesday');
    
    $date = new DateTime('today');
    while (!in_array($date->format('l'), $scheduleWindow)) {
        $date->add(new DateInterval('P1D'));
    }
    
    echo "Next date to contact is " . $date->format('Y-m-d H:i:s');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Was wondering if somebody could please help me solve this. I've follwed all steps
i was wondering if somebody could help me out. I have a plan of
I was wondering if somebody could help me with how exactly to write some
Hi guys just wondering if somebody could help me try and correctly thread my
I was wondering if somebody could help me, I can do it on pen
I was wondering if somebody could shed some light on this browser behaviour: I
I'm learning Javascript (mainly using JQuery) and was wondering if somebody could help me.
I have a small issue I was hoping somebody could help me with. I
Wondering if anyone can help me with this annoying but trivial (in terms of
I was wondering if somebody could explain me how pointers and string parsing works.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.