How to put If statement in printf ?

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

Post Reply
valve79
Forum Newbie
Posts: 1
Joined: Thu Sep 26, 2013 8:30 am

How to put If statement in printf ?

Post 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.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: How to put If statement in printf ?

Post 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 ;-)
Post Reply