Loop from array - please HELP

Discussion in 'PHP' started by KingCobra, Oct 2, 2011.

  1. #1
    Dear friends,

    I need to produce just some dates from a whole year. I will provide numbers (0-6) from form submission. 0-6 means Numeric representation of the day of the week, Sunday to Saturday.

    Example:

    Input:

    $myDate = "3, 4, 6"; // Suppose I got this number form submitted form where it means Wednesday, Thursday & Saturday.
    $startDate = 10/03/2011 // mm-dd-yyyy
    $endDate = 10/02/2012 // mm-dd-yyyy (one year based on $startDate)

    Output: (it will generate dates for one year, I just show you some)

    10/05/2011 Wednesday
    10/06/2011 Thursday
    10/08/2011 Saturday
    10/12/2011 Wednesday
    10/13/2011 Thursday
    10/15/2011 Saturday
    ---- continue -----
    10/29/2011 Saturday
    11/02/2011 Wednesday
    11/03/2011 Thursday
    11/05/2011 Saturday
    ---- continue -----
    09/26/2012 Wednesday
    09/27/2012 Thursday
    09/29/2012 Saturday

    Note:
    $myDate can be any value from 0 to 6. Suppose (0, 5) or (0, 1, 2, 3, 4) or (6) etc. any combination.
    You can reconstruct $myDate to keep the values according to your needs.

    Thank you.
     
    KingCobra, Oct 2, 2011 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Just use while, work out the dates, add 7 and loop round until the tested date is >= the end date.
     
    sarahk, Oct 2, 2011 IP
  3. mpchekuri

    mpchekuri Member

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    26
    #3
    Try this

    $days = explode(',',$myDate);

    foreach($days as $day){
    $startday = time();
    $endday = $startday + (3600 * 24 * 365);
    switch($day){
    case 0:
    for($starttime=$startday; $starttime < $endday; $starttime += (3600 * 24)){
    if(date('D', $starttime) === 'Sunday')
    echo date('m/d/Y D');
    } // Repeat the same for each case
    break;
    case 1 :
    break;
    case 2:
    break;
    case 3:
    break;
    case 4:
    break;
    case 5:
    break;
    case 6:
    break;
    }
    }
     
    mpchekuri, Oct 3, 2011 IP
  4. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    I am still waiting for your HELP friends.
     
    KingCobra, Oct 3, 2011 IP
  5. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    mpchekuri,

    Thanks for your help. But your code showing nothing. Would you please check again?
     
    KingCobra, Oct 3, 2011 IP
  6. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #6
    To honoring your time I will pay you a little amount $10.00 for this solution.
    Please don't mind as i have no ability to give you a handsome amount.
     
    KingCobra, Oct 4, 2011 IP
  7. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #7
    SOLVED

    I solved it by myself.

    Thank you all.
     
    KingCobra, Oct 6, 2011 IP