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

date_format and "+2 hours" problems

 
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 -> Smarty Development
View previous topic :: View next topic  
Author Message
drdrone
Smarty n00b


Joined: 27 Jul 2010
Posts: 4

PostPosted: Tue Jul 27, 2010 10:31 pm    Post subject: date_format and "+2 hours" problems Reply with quote

I have this code in my template in a section loop:

{$content_items[i].created|date_format:"%A, %d %B %Y %R"}

which outputs for sample: Wednesday, 14 July 2010 07:32.

Now I would like the time to change +2 hours.

I have tried everything and this simply does not work. I thought it should look like:

{$content_items[i].created|"+2 hour"|date_format:"%A, %d %B %Y %R"}

If I remove my content_items variable it works with the current time.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Jul 28, 2010 12:41 am    Post subject: Reply with quote

If your value is a timestamp:

Code:
{$content_items[i].created+7200|date_format:"%A, %d %B %Y %R"}
Back to top
View user's profile Send private message Visit poster's website
drdrone
Smarty n00b


Joined: 27 Jul 2010
Posts: 4

PostPosted: Wed Jul 28, 2010 9:43 am    Post subject: Reply with quote

No, then I get Thursday, 01 January 1970 03:33

my var $content_items[i].created looks like this without any date_format:
2010-07-14 07:32:45
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Jul 28, 2010 1:39 pm    Post subject: Reply with quote

is your value a unix timestamp? (seconds since epoch)
Back to top
View user's profile Send private message Visit poster's website
drdrone
Smarty n00b


Joined: 27 Jul 2010
Posts: 4

PostPosted: Wed Jul 28, 2010 9:39 pm    Post subject: Reply with quote

No its not. $content_items[i].created fetches content from mysql and the tables contains 2010-07-14 07:32:45
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Jul 28, 2010 9:46 pm    Post subject: Reply with quote

If you want to do this in template, you need to convert that to a timestamp. you'd be better of handling it in a plugin. But there are probably ways to get around it. maybe:

Code:
{assign var="timestamp" value=$content_items[i].created|strtotime}
{$timestamp+7200|date_format:"%A, %d %B %Y %R"}


Or, select as a timestamp from MySQL in the first place:

Code:
select UNIX_TIMESTAMP(created) as timestamp from mytable


You could even select the 2 hour interval from MySQL datetime too, if that is the only interval you are after.

As a general rule of thumb, dates are much more flexible to the template engine as datestamps. Then they are just data, not a pre-determined format.
Back to top
View user's profile Send private message Visit poster's website
drdrone
Smarty n00b


Joined: 27 Jul 2010
Posts: 4

PostPosted: Wed Jul 28, 2010 11:03 pm    Post subject: Reply with quote

It works as a charm after I converted it to a timestamp directly in the template. Thanks a lot for your help! Smile
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 -> Smarty Development 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