SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
Thread: Array in a for loop
-
Apr 12, 2009, 12:37 #1
- 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
}
-
Apr 12, 2009, 12:50 #2
- Join Date
- Mar 2002
- Location
- Bristol, UK
- Posts
- 2,240
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Create an array of what, exactly?
-
Apr 12, 2009, 12:51 #3
- 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
-
Apr 12, 2009, 12:55 #4
- 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);
-
Apr 12, 2009, 13:32 #5
- 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;
}
-
Apr 12, 2009, 13:43 #6
- 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.
-
Apr 13, 2009, 03:17 #7
- 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);
}
// ...
-
Apr 13, 2009, 03:28 #8
- 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.
-
Apr 13, 2009, 03:35 #9
- Join Date
- Feb 2008
- Location
- end($world)
- Posts
- 834
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Bookmarks