Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[SOLVED] Using FOR loop in controller


Go to End


3 Posts   1007 Views

Avatar
Optic Blaze

Community Member, 190 Posts

26 January 2014 at 12:37am

Hi there,

I want to dynamically create a list of times that will be used in an array to populate a drop down menu with times in half hour increments. It looks like this so far:

// Times drop down array
for($count = 1; $count <=8; $count++) {
$starttime1 = strtotime("08:00");
$interval = 30;
$times = array(date("H:i", strtotime('+'.($count*$interval).'minutes', $starttime1)));
}

When i do a 'print_r($times)' it prints:
Array ( [0] => 12:00 )
instead of
Array ( [0] => 08:00, [1] => 08:30, [3] => 09:00, [4] => 09:30...... )

What am i doing wrong

Avatar
thomas.paulson

Community Member, 107 Posts

26 January 2014 at 3:36am

$times[] = date("H:i", strtotime('+'.($count*$interval).'minutes', $starttime1));

Avatar
Optic Blaze

Community Member, 190 Posts

26 January 2014 at 11:37am

Thanks for that...worked like a charm. Cant believe i did not think of it.