Reputation: 5483
I am populating a simple HTML select
list in a Smarty template with month names. A timestamp object is passed to the template. I would like the passed in timestamp month to be the default value of the select
. The passed value could be passed in as an int
(1,2,3,etc...) or a string
(jan,feb,mar,etc...).
To set a default, selected value I need to compare the passed in timestamp month to a timestamp I created in the template. For example, to compare to just the current month I would do something like this:
<option {if $smarty.now|date_format:"%b" = $passedInTimestamp['month']}selected{/if}>
text
</option>
$smarty.now
will only work for the current month obviously. My <option>
tag is inside a for loop, looping from 1 to 12. How can I create a timestamp with a specific month to compare to the passed in month?
Upvotes: 0
Views: 3847
Reputation: 2643
If I understood well, this is what you need :
<select>
{for $i=1 to 12}
<option {if '1-'|@cat:$i:'-':'Y'|date|@strtotime|date_format:"%b" == $passedInTimestamp['month']}selected{/if}>
{$i}
</option>
{/for}
</select>
First, I created a date string for months using cat modifier :
{'1-'|@cat:$i:'-':'Y'|date}
// will output
// 1-1-2013 1-2-2013 1-3-2013 ....
Then I used strtotime modifier to convert it to timstamp
{'1-9-2013'|@strtotime}
// will output 1377986400
Finally I used date_format:"%b"
as you did above to compare it to your passed in timestamp month.
Upvotes: 1