Page 1 of 1

How to put If statement in printf ?

Posted: Thu Sep 26, 2013 8:37 am
by valve79
I have a following problem -
I need to select current time from dropdown thats rounded off to 15 minutes.

The problem i need to select the time and i cant figure out how to select it using if statement.

THis is what i have
$rounded = "current time";
<select name="selTimeAt" class="select-combo" id="selTimeAt">
<?php
$start=strtotime('00:00');
$end=strtotime('23:45');
for ($halfhour=$start;$halfhour<=$end;$halfhour=$halfhour+15*60) {
printf('<option value="%s">%s</option>',date('H:i',$halfhour),date('g:i a',$halfhour));
}
?>
</select>

In printf i assumed i just add if tatement and done but i been tearing my hair out and got result of none.

If any one can help i would greatly appreciate it.

Re: How to put If statement in printf ?

Posted: Thu Sep 26, 2013 9:31 am
by twinedev

Code: Select all

printf('<option  %s value="%s">%s</option>',
        ((time()>=$halfhour && time()<=$halfhour+899) ? 'selected="selected"' : ''),
        date('H:i',$halfhour),
        date('g:i a',$halfhour)."[$halfhour]"
       );

just a side note, those are $quarterhours, not $halfhours ;-)