SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Printing next date within an array

    Hi,

    Im a complete novice when it comes to php but im trying to print the next date within an array. I have the following code but im not sure im anywhere near:

    <?php function get_next_dressdown() {
    // Dress Down Days
    $dress = array(
    '7-Jan-2011',
    '4-Feb-2011',
    '18-Mar-2011',
    '1-Apr-2011',
    '6-May-2011',
    '3-Jun-2011',
    '1-Jul-2011',
    '5-Aug-2011',
    '2-Sep-2011',
    '7-Oct-2011',
    '4-Nov-2011',
    '2-Dec-2011',
    );
    $current = time()
    if ($dress>=$current) {
    echo date("d<\s\u\p>S</\s\u\p> F Y",$dress);
    break;
    }
    ?></p>

    Anyone help me out or point me in the right direction?

    Thanks,

    Mike

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    A couple of things to note:
    • The dates in the array aren't really in a viable format to be compared to the current time. It'd be better if they were in a timestamp format, i.e. an integer storing the amount of seconds elapsed since 1st Jan 1970. So when you're comparing them, use mktime() to get the date in the array as a number. Then use the time() function to get the current timestamp to compare.
    • You're trying to compare the whole array with the current time - so use a loop to compare each value - foreach with a break to stop it when it's found a suitable date.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Jake,

    Thanks for the info so am i along the right lines with this:

    <?php
    // Dress Down Days
    $dress = array(
    '07/01/2011',
    '04/02/2011',
    '18/03/2011',
    '01/04/2011',
    '06/05/2011',
    '03/06/2011',
    '01/07/2011',
    '05/08/2011',
    '02/09/2011',
    '07/10/2011',
    '04/11/2011',
    '02/12/2011',
    );
    $date = date( "d/m/Y", time() );
    foreach ($dress as $value >= $date) {
    echo date("d<\s\u\p>S</\s\u\p> F Y",$date);
    break;
    }
    ?>

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Kind of. I'll give you a hand :P

    PHP Code:
    function get_next_dressdown(){
        
    $dress = array(
            
    '07/01/2011'
            
    '04/02/2011'
            
    '18/03/2011',
            
    '01/04/2011',
            
    '06/05/2011',
            
    '03/06/2011',
            
    '01/07/2011',
            
    '05/08/2011',
            
    '02/09/2011',
            
    '07/10/2011',
            
    '04/11/2011',
            
    '02/12/2011'
        );
        
    $currentTime time();
        foreach (
    $dress as $date){
            
    $dateParts explode('/'$date);
            
    $dressTime mktime(000$dateParts[1], $dateParts[0], $dateParts[2]);
            if(
    $dressTime $currentTime){
                return 
    $dressTime;
                break;
            }
        }
        return 
    false;

    The way you'd use this:
    PHP Code:
    $nextDate get_next_dressdown();
    if(
    $nextDate === false){
        echo 
    'No more dressdowns';
    }else{
        echo 
    'Next dressdown: 'date("d<\s\u\p>S</\s\u\p> F Y"$nextDate);

    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Jake you are a legend. Wasnt even close!! :-p

    On a seperate matter my rss feeds have stopped showing. Ive checked that the rss feeds are up and they are. Could you take a look at this for me and see if you can see anything obvious:

    <p id="weather"><?php
    $rs = $rss->get('http://xml.weather.yahoo.com/forecastrss?p=UKXX1413&u=c');
    echo "<b>Today's Weather</b><br /><span class='indent'>";

    $desc = explode("<br />",str_replace("<BR />","<br />",$rs['items'][0]['description']));
    echo str_replace("<img","<img alt=''",$desc[0]);
    echo str_replace(",","<br />",$desc[2]);
    echo "</span>";
    ?></p>

    Last one i promise :-)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •