Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional based on day of week

Tags:

smarty

I would like to print out a different string of text based on the current day of the week. Any advice?

Am I on the right track with this:

{if $current_timestamp|date_format:"%A" == 'Monday'}
    Monday
{elseif $current_timestamp|date_format:"%A" == 'Tuesday'}
    Tuesday
{elseif $current_timestamp|date_format:"%A" == 'Wednesday'}
    Wednesday
{elseif $current_timestamp|date_format:"%A" == 'Thursday'}
    Thursday
{elseif $current_timestamp|date_format:"%A" == 'Friday'}
    Friday
{else}
    Weekend
{/if}

This is currently working for me but I was wondering if there's a more efficient way.

P.S. I won't be printing out the name of the day of the week, that's just my example.

like image 583
conbask Avatar asked Dec 13 '25 05:12

conbask


1 Answers

you should learn to "cache" your values:

{assign var="weekday" value=$smarty.now|date_format:"%u"}
{if $weekday == 1} Monday
…
{elseif $weekday == 7} Sunday
{/if}
like image 80
rodneyrehm Avatar answered Dec 16 '25 21:12

rodneyrehm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!