|
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 |
omexlu Smarty Rookie
Joined: 13 Mar 2014 Posts: 27
|
Posted: Sun May 21, 2017 6:47 am Post subject: Count passed days from date? |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun May 21, 2017 6:56 pm Post subject: |
|
|
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 |
|
omexlu Smarty Rookie
Joined: 13 Mar 2014 Posts: 27
|
Posted: Sun May 21, 2017 7:12 pm Post subject: |
|
|
Hi,
I am not really the php profi
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
|
Back to top |
|
omexlu Smarty Rookie
Joined: 13 Mar 2014 Posts: 27
|
Posted: Mon May 22, 2017 3:14 am Post subject: |
|
|
Very easy
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 |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon May 22, 2017 5:43 am Post subject: |
|
|
Why not wrap everything into function? Why pollute template with that ridiculous calculation? |
|
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
|