PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
impulse()
Forum Regular
Posts: 748 Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:
Post
by impulse() » Mon Feb 05, 2007 9:04 am
I've just created an array of minutes in an hour using the range function:
But the output shows:
0
1
2
3
4
5
6
7
8
9
10
11 etc
How is this possible to change to:
00
01
02
03
04
05
06
07
08
09
10
11 etc?
Without creating another loop to replace all values lower than 10?
At the moment I have the following code:
Code: Select all
$minute = array(00,01,02,03,04,05,06,07,08,09);
for ($i = 10; $i < 60; $i++) {
array_push($minute, $i);
}
Regards,
Last edited by
impulse() on Mon Feb 05, 2007 9:27 am, edited 1 time in total.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Feb 05, 2007 9:16 am
Without being in strings, a leading zero tells PHP to parse the numbers as octal. 08 and 09 will result in zeros.
Use
sprintf() during output.
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Mon Feb 05, 2007 9:18 am
Why do you need them in the array? I'd put them into the array as integers so all the maths I do with them works properly, and then display them on the front end using str_pad() or sprintf() or ((strlen($x)<2)?"0".$x:$x) or something.
impulse()
Forum Regular
Posts: 748 Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:
Post
by impulse() » Mon Feb 05, 2007 9:26 am
Sprintf sorted the problem using the following:
The reason they're in an array it to make it simple for me to do
and I have the numbers I need, easily. I'm then using a foreach loop and created a dropdown box.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Feb 05, 2007 9:39 am
A for() loop would be more straight forward and use less memory.
visitor-Q
Forum Commoner
Posts: 72 Joined: Mon Feb 05, 2007 1:40 am
Post
by visitor-Q » Mon Feb 05, 2007 6:49 pm
did no one think of this?
Code: Select all
<?php
$minutes = range(0, 59);
foreach($minutes as $minute){
(($minute < 10) ? ($minute = 0 . $minute));
echo $minute ."<br />\n";
}
?>
is it not the same? is it better, or worse?
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Tue Feb 06, 2007 4:04 am
visitor-Q wrote: is it better, or worse?
It doesn't work, so I guess that makes it worse.
visitor-Q
Forum Commoner
Posts: 72 Joined: Mon Feb 05, 2007 1:40 am
Post
by visitor-Q » Tue Feb 06, 2007 9:25 am
onion2k wrote: visitor-Q wrote: is it better, or worse?
It doesn't work, so I guess that makes it worse.
sorry, most of my code is untested, and mainly for a theoretical example. this works...
Code: Select all
<?php
$minutes = range(0, 59);
foreach($minutes as $minute){
(($minute < 10) ? ($minute = 0 . $minute) : ('')); //forgot that last bit on this line
echo $minute ."<br />\n";
}
?>