|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
drdrone Smarty n00b
Joined: 27 Jul 2010 Posts: 4
|
Posted: Tue Jul 27, 2010 10:31 pm Post subject: date_format and "+2 hours" problems |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Jul 28, 2010 12:41 am Post subject: |
|
|
If your value is a timestamp:
Code: | {$content_items[i].created+7200|date_format:"%A, %d %B %Y %R"} |
|
|
Back to top |
|
drdrone Smarty n00b
Joined: 27 Jul 2010 Posts: 4
|
Posted: Wed Jul 28, 2010 9:43 am Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Jul 28, 2010 1:39 pm Post subject: |
|
|
is your value a unix timestamp? (seconds since epoch) |
|
Back to top |
|
drdrone Smarty n00b
Joined: 27 Jul 2010 Posts: 4
|
Posted: Wed Jul 28, 2010 9:39 pm Post subject: |
|
|
No its not. $content_items[i].created fetches content from mysql and the tables contains 2010-07-14 07:32:45 |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Jul 28, 2010 9:46 pm Post subject: |
|
|
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 |
|
drdrone Smarty n00b
Joined: 27 Jul 2010 Posts: 4
|
Posted: Wed Jul 28, 2010 11:03 pm Post subject: |
|
|
It works as a charm after I converted it to a timestamp directly in the template. Thanks a lot for your help! |
|
Back to top |
|
|
|
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
|