Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Timebased content using Smarty logic

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Njit
Smarty n00b


Joined: 08 Jan 2017
Posts: 4

PostPosted: Sun Jan 08, 2017 6:02 pm    Post subject: Timebased content using Smarty logic Reply with quote

Hi,

i'm using Smarty for my website. I'm trying to show some different content based on the time of day. But i'm doing something wrong with the logic i think,

I want to use these 3 timeframes (based on 24 hour period)

morning: 0600 - 1200
Afternoon: 1200 - 1800
Evening: 1800 - 0600

My Smarty looks like this:

Code:
<!-- It is now {$smarty.now|date_format:"%H:%M"} -->

{if $smarty.now|date_format:"%H" >= 06 and $smarty.now|date_format:"%H" < 12 }{Showtime show='7'}
{elseif $smarty.now|date_format:"%H" >= 12 and $smarty.now|date_format:"%H" < 18}{Showtime show='8'}
{elseif $smarty.now|date_format:"%H" >= 18 and $smarty.now|date_format:"%H" < 06}{Showtime show='9'}{/if}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Jan 08, 2017 8:13 pm    Post subject: Reply with quote

Don't forget to flag it as `nocache`.
Back to top
View user's profile Send private message
Njit
Smarty n00b


Joined: 08 Jan 2017
Posts: 4

PostPosted: Sun Jan 08, 2017 8:39 pm    Post subject: Reply with quote

I think thats not part of the problem. The problem is with the if's
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Jan 08, 2017 8:42 pm    Post subject: Reply with quote

What problem?
Back to top
View user's profile Send private message
Njit
Smarty n00b


Joined: 08 Jan 2017
Posts: 4

PostPosted: Sun Jan 08, 2017 8:44 pm    Post subject: Reply with quote

Content is not shown during certain time-periods.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Mon Jan 09, 2017 12:14 am    Post subject: Reply with quote

We will look at the logic shortly.

My first concern is whether Smarty gives the output of the date_format modifier as a string or as an integer.

The Smarty docs use the phrase:
%H - hour as a decimal number using a 24-hour clock (range 00 to 23)

The docs do not say one way or the other, but because Smarty generates output into what is being built up as a string, the output is probably string.

Still, PHP can handle comparisons easily, but can sometimes get confused when comparing strings against numbers. So, please try comparing the date_format modifier result to a string, OR casting the output as an integer.

It might not make a difference.

Now, to the logic. (XX is the output):

XX>=18 && XX<6

If XX is 23, the first expression is true but the second is false.

Between 6 and 12 will work, and between 12 and 18 will work.

But the last condition can be a default (if not in the prior two conditions, then it MUST be in the third).

However, if a condition test must exist, then the output value will be greater than or equal to 18, OR it will be less than 6. It can't be both.
Back to top
View user's profile Send private message
Njit
Smarty n00b


Joined: 08 Jan 2017
Posts: 4

PostPosted: Mon Jan 09, 2017 5:26 am    Post subject: Reply with quote

Output of the date works fine like this.

Thanks. Missed the OR..
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP