SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array in a for loop

    Hi guys

    How can I create a array in this loop

    PHP Code:
    $current time();
    for(
    $i 0$i 60$i++){

    $current strtotime("+1 day"$current);


    //// I want to create the array here



  2. #2
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Create an array of what, exactly?

  3. #3
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the reply

    The loop creates a list of the next 60 days

    I need to pass it to a template file so need to put it in to a array

  4. #4
    SitePoint Zealot kamm's Avatar
    Join Date
    Nov 2003
    Location
    Espaņa
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean this?

    PHP Code:
    $current time();
    for(
    $i 0$i 60$i++){

    $current strtotime("+1 day"$current);

    $temp_array[] = $current;
    echo
    "$current<br />";

    }
    echo 
    '<pre>';
    print_r($temp_array); 

  5. #5
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks that worked

    Now getting this error

    Warning: strtotime() expects parameter 2 to be long, string given in

    PHP Code:
    $current time();

    for(
    $i 0$i 60$i++){



    $current1 strtotime("+1 day"$current);

    $current date("F j, Y, g:i a"$current1); 

    $temp_array[] = $current;





  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $iCurrent 
    time();
    for(
    $iDays 0$iDays 60$iDays++)
    {
        
    $aFinal[] = date('F j, Y, g:i a'$iCurrent += 86400);
    }
    print_r($aFinal);
    /*
    Array
    (
        [0] => April 13, 2009, 9:42 pm
        [1] => April 14, 2009, 9:42 pm
        [2] => April 15, 2009, 9:42 pm
        [3] => April 16, 2009, 9:42 pm
        [4] => April 17, 2009, 9:42 pm
        [5] => April 18, 2009, 9:42 pm
        [6] => April 19, 2009, 9:42 pm
        [7] => April 20, 2009, 9:42 pm
        [8] => April 21, 2009, 9:42 pm
        [9] => April 22, 2009, 9:42 pm
        [10] => April 23, 2009, 9:42 pm
        [11] => April 24, 2009, 9:42 pm
        [12] => April 25, 2009, 9:42 pm
        [13] => April 26, 2009, 9:42 pm
        [14] => April 27, 2009, 9:42 pm
        [15] => April 28, 2009, 9:42 pm
        [16] => April 29, 2009, 9:42 pm
        [17] => April 30, 2009, 9:42 pm
        [18] => May 1, 2009, 9:42 pm
        [19] => May 2, 2009, 9:42 pm
        [20] => May 3, 2009, 9:42 pm
        [21] => May 4, 2009, 9:42 pm
        [22] => May 5, 2009, 9:42 pm
        [23] => May 6, 2009, 9:42 pm
        [24] => May 7, 2009, 9:42 pm
        [25] => May 8, 2009, 9:42 pm
        [26] => May 9, 2009, 9:42 pm
        [27] => May 10, 2009, 9:42 pm
        [28] => May 11, 2009, 9:42 pm
        [29] => May 12, 2009, 9:42 pm
        [30] => May 13, 2009, 9:42 pm
        [31] => May 14, 2009, 9:42 pm
        [32] => May 15, 2009, 9:42 pm
        [33] => May 16, 2009, 9:42 pm
        [34] => May 17, 2009, 9:42 pm
        [35] => May 18, 2009, 9:42 pm
        [36] => May 19, 2009, 9:42 pm
        [37] => May 20, 2009, 9:42 pm
        [38] => May 21, 2009, 9:42 pm
        [39] => May 22, 2009, 9:42 pm
        [40] => May 23, 2009, 9:42 pm
        [41] => May 24, 2009, 9:42 pm
        [42] => May 25, 2009, 9:42 pm
        [43] => May 26, 2009, 9:42 pm
        [44] => May 27, 2009, 9:42 pm
        [45] => May 28, 2009, 9:42 pm
        [46] => May 29, 2009, 9:42 pm
        [47] => May 30, 2009, 9:42 pm
        [48] => May 31, 2009, 9:42 pm
        [49] => June 1, 2009, 9:42 pm
        [50] => June 2, 2009, 9:42 pm
        [51] => June 3, 2009, 9:42 pm
        [52] => June 4, 2009, 9:42 pm
        [53] => June 5, 2009, 9:42 pm
        [54] => June 6, 2009, 9:42 pm
        [55] => June 7, 2009, 9:42 pm
        [56] => June 8, 2009, 9:42 pm
        [57] => June 9, 2009, 9:42 pm
        [58] => June 10, 2009, 9:42 pm
        [59] => June 11, 2009, 9:42 pm
    )
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a note, arrays should be declared before being used in a loop:
    PHP Code:
    $aFinal = array();
    $iCurrent time();
    for (
    $iDays 0$iDays 60$iDays++) {
        
    $aFinal[] = date('F j, Y, g:i a'$iCurrent += 86400);
    }
    // ... 

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Good practice? Indeed!

    Required? Not really.

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Good practice? Indeed!

    Required? Not really.

    True, you will get a warning when you use error_reporting(E_ALL); though.


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
  •