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

Smarty: Time-Modifications

 
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
omexlu
Smarty Rookie


Joined: 13 Mar 2014
Posts: 27

PostPosted: Sat May 05, 2018 3:12 am    Post subject: Smarty: Time-Modifications Reply with quote

Hello,

I have a variabel {$getRow.date} (comes from a foreach loop) who contains a mysql datestamp and a second variable {$getRow.time} who contrains a string (example: 11 [min.]).

How can i now subract '11 minutes' from the first variable and get the new timestamp that i can formate with date_format?

PS. Sorry for my bad english i hope you understand me Smile

Thanks in advance.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


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

PostPosted: Sat May 05, 2018 6:48 am    Post subject: Reply with quote

You can try:
Code:
{"`$getRow.date` -`$getRow.time`"|strtotime|date_format:"%c"}

You will need to make sure the 'time' string is understandable to PHP.

I think "11 [min]" is not understandable, but "11 minutes" is understandable.

Everything starts as a string, but embedded Smarty variables are escaped with backticks.
Back to top
View user's profile Send private message
omexlu
Smarty Rookie


Joined: 13 Mar 2014
Posts: 27

PostPosted: Sat May 05, 2018 9:25 am    Post subject: Reply with quote

Hello,

Thanks for you reply.

$getRow.time CONTAINS ONLY a number = 11

Maybe somthing like that is possible?:
Code:
{"`$getRow.date` -`$getRow.time` minutes"|strtotime|date_format:"%c"}


I will give it a try this evening or tomorrow.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat May 05, 2018 1:17 pm    Post subject: Reply with quote

Write a wrapper function that uses DateTime and DateInterval classes to manipulate times.

omexlu wrote:
Maybe somthing like that is possible?:
Code:
{"`$getRow.date` -`$getRow.time` minutes"|strtotime|date_format:"%c"}

It is possible, but very dirty and prone to failures..
Back to top
View user's profile Send private message
omexlu
Smarty Rookie


Joined: 13 Mar 2014
Posts: 27

PostPosted: Sat May 05, 2018 1:32 pm    Post subject: Reply with quote

Hi,

After some testings i think i have found a better way to do this with math function:
Code:
{math equation="a - (60 * b)" a=$getRow.date|strtotime b="10" assign="finalTime"}{$finalTime|date_format:"%H:%M"}


Here i get the correct results of that row -10 minutes.

What do you think about?

EDIT: After do the script all works like a charm Smile
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat May 05, 2018 9:24 pm    Post subject: Reply with quote

I already said what I think about it. Dirty hack built on presumptions.
Back to top
View user's profile Send private message
scuzzy
Smarty Regular


Joined: 31 Aug 2003
Posts: 84

PostPosted: Mon May 07, 2018 10:18 pm    Post subject: Reply with quote

omexlu wrote:
What do you think about?


If you had to write that code 50 times in various templates, you'd quickly move it to a modifier function I think Shocked https://www.smarty.net/docs/en/plugins.modifiers.tpl
Back to top
View user's profile Send private message Visit poster's website
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