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

Count passed days from date?

 
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: Sun May 21, 2017 6:47 am    Post subject: Count passed days from date? Reply with quote

Hello,

I have the follwing in a foreach from mysql:
Code:
{foreach $rowsEntryLiquids as $rowEntryLiquids}
 {$rowEntryLiquids.date|date_format:"%d / %m / %Y"}
{/foreach}


Now i want to show the days passed from date to now:
Example: 8 days ago

How can i do that in tpl?

Thanks
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun May 21, 2017 6:56 pm    Post subject: Reply with quote

Use custom date formatter.
See Smarty::registerPlugin() and this example:
Code:
$nf = new NumberFormatter("ru_RU", NumberFormatter::CURRENCY);
$cs = ini_get('default_charset');
$tpl->registerPlugin('modifier', 'price',
  function($price, $currency = 'RUB')
  use($nf, $cs)
  {
    return iconv('UTF-8', $cs, $nf->formatCurrency($price, $currency));
  }
);
Back to top
View user's profile Send private message
omexlu
Smarty Rookie


Joined: 13 Mar 2014
Posts: 27

PostPosted: Sun May 21, 2017 7:12 pm    Post subject: Reply with quote

Hi,

I am not really the php profi Smile

I don't know how to implement that if i have the foreach already in the tpl (date).

Maybe you can give more informations or maybe a functional example?

thx
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun May 21, 2017 10:11 pm    Post subject: Reply with quote

I'm not going to write your code for you, if that was what you were asking for.

Start with http://www.smarty.net/docs/en/plugins.tpl
Then http://www.smarty.net/docs/en/plugins.modifiers.tpl for your specific case and finally for date formatting, look at the http://php.net/manual/en/class.intldateformatter.php
Plus the example above to register it.
Back to top
View user's profile Send private message
omexlu
Smarty Rookie


Joined: 13 Mar 2014
Posts: 27

PostPosted: Mon May 22, 2017 3:14 am    Post subject: Reply with quote

Very easy Smile

First:
Code:
// create modifer
$smarty->registerPlugin("modifier","date_timestamp", "print_current_date");

function print_current_date($stringA)
{
     $timestamp = strtotime($stringA);
    return $timestamp;
}


in tpl:
Code:
{math equation="a - b" a=$smarty.now b=$rowEntryLiquids.date|date_timestamp assign="dateDiff"}
            {math equation="floor(a / (60 * 60 * 24) )" a=$dateDiff}


finish Smile
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon May 22, 2017 5:43 am    Post subject: Reply with quote

Why not wrap everything into function? Why pollute template with that ridiculous calculation?
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