Please FIX this small PHP code for me (Quick date manipulation)

Discussion in 'PHP' started by medaa25, Mar 24, 2010.

  1. #1
    Hi,

    I have found this php code that provides the date of any upcoming weekdays. I need to change the target week day (monday, tuesday etc )but I am unsure of how to do it.

    Can you please tell me how. Your help will be very much appreciated. Cheers.


    
    <?
    function next_week($target_week_day)
             {
             $date = getDate();
    
             $day = $date["mday"];
             $week_day = $date["wday"];
             $month = $date["mon"];
             $year = $date["year"];
    
             //This assumes that if today is a target week day,
             //today's date will be used and not next week's.
             //To change that, just make
             //if($week_day <= $target_week_day)
             //if($week_day < $target_week_day)
             if($week_day <= $target_week_day)
                $days_left = $target_week_day - $week_day;
             else
                $days_left = 7 - ($week_day - $target_week_day);
    
             //This script works by finding out the number of days separating
             //the current date and the next target week day.
             $next_week = getDate(mktime(0, 0, 0, $month, $day + $days_left, $year));
    
             $next_week_html = $next_week["month"] . " " . $next_week["mday"] . " , " . $next_week["year"];
    
             return($next_week_html);
             }
    ?>
    
    PHP:
     
    medaa25, Mar 24, 2010 IP
  2. pig2cat

    pig2cat Active Member

    Messages:
    299
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Hi,
    this script seems to work with integers(0 to 6) as far as I can see

    use
    
    echo next_week(0);  //print next sunday
    echo next_week(2); //print next tuesday
    echo next_week(5); //print next friday
    
    PHP:
    I hope this is what you meant
     
    pig2cat, Mar 24, 2010 IP
  3. medaa25

    medaa25 Active Member

    Messages:
    388
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    60
    #3
    I tried but it didn't work.
    Do you know what could be wrong? I think the way I called the target date using your code is wrong .
    I am noob at php as you could tell.




    This is what i put in my source code:

    
    <?
    function next_week($target_week_day)
             {
             $date = getDate();
    
             $day = $date["mday"];
             $week_day = $date["wday"];
             $month = $date["mon"];
             $year = $date["year"];
    
             //This assumes that if today is a target week day,
             //today's date will be used and not next week's.
             //To change that, just make
             //if($week_day <= $target_week_day)
             //if($week_day < $target_week_day)
             if($week_day <= $target_week_day)
                $days_left = $target_week_day - $week_day;
             else
                $days_left = 7 - ($week_day - $target_week_day);
    
             //This script works by finding out the number of days separating
             //the current date and the next target week day.
             $next_week = getDate(mktime(0, 0, 0, $month, $day + $days_left, $year));
    
             $next_week_html = $next_week["month"] . " " . $next_week["mday"] . " , " . $next_week["year"];
    
             return($next_week_html);
             }
    
    
    
    ?>
    
    <?php echo next_week(5); //print next friday ?>
    
    
    PHP:
     
    medaa25, Mar 25, 2010 IP
  4. mnvlxxx

    mnvlxxx Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Looks like it is working fine. Added $next_week["weekday"] to display day name.

    
    <?
    function next_week($target_week_day)
             {
             $date = getDate();
    
             $day = $date["mday"];
             $week_day = $date["wday"];
             $month = $date["mon"];
             $year = $date["year"];
    
             //This assumes that if today is a target week day,
             //today's date will be used and not next week's.
             //To change that, just make
             //if($week_day <= $target_week_day)
             //if($week_day < $target_week_day)
             if($week_day <= $target_week_day)
                $days_left = $target_week_day - $week_day;
             else
                $days_left = 7 - ($week_day - $target_week_day);
    
             //This script works by finding out the number of days separating
             //the current date and the next target week day.
             $next_week = getDate(mktime(0, 0, 0, $month, $day + $days_left, $year));
    
             $next_week_html = $next_week["weekday"] . " " . $next_week["month"] . " " . $next_week["mday"] . " , " . $next_week["year"];
    
             return($next_week_html);
             }
    
    
    
    ?>
    
    <?php echo next_week(5); //print next friday ?>
    
    PHP:
     
    mnvlxxx, Mar 25, 2010 IP
  5. pig2cat

    pig2cat Active Member

    Messages:
    299
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Medaa
    you commented out the php closure ?>


    
    
    <?php 
    echo next_week(5); //print next friday (stuff behind this on this line doesn't get read)
    ?>
    
    
    PHP:
     
    pig2cat, Mar 25, 2010 IP
    medaa25 likes this.
  6. mnvlxxx

    mnvlxxx Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Sorry. I think it wouln't make any difference on this situation anyway :)
     
    mnvlxxx, Mar 25, 2010 IP
  7. medaa25

    medaa25 Active Member

    Messages:
    388
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Found the solution. It was because the beginning of the code didn't start by <?php ... (using wordpress on localhost)

    Thank for your help, your code works + rep
     
    medaa25, Mar 25, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    As I've mentioned quite a few times here, always use full php tags, as not always will short tags (<?) be enabled by default (which can cause compatibility issues - especially if on shared hosting/no php.ini access), furthermore since your on localhost locate your php.ini, edit it - to enable short tags by setting it to On then restart Apache.
     
    danx10, Mar 25, 2010 IP