alouty
February 20, 2014, 4:13pm
1
I would like how to set [[$listing.activation_date]] to:
If less than 1 min than show how many mins from now;
If less than 1 day then show how many hours from now;
If more than 1 day than show how many days from now
{assign var="startDate" value = $smarty.now}
{assign var="endDate" value = $listing.activation_date|strtotime}
{math assign="diff" equation="(st - en)" st=$startDate en=$endDate }
<div class="post-block-out featured">
<div class="post-block feature-bg1">
{if $diff < 86400}<span class="new-ribbon-list"></span>{/if}
</div> </div>
<div style="text-align:center;">[[$listing.activation_date]]</div>
xMog
February 22, 2014, 3:30am
2
How about something like this:
<?php
function ago($time)
{
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$difference = $now - $time;
$tense = "ago";
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] 'ago' ";
}
$var = ago(time() - 10);
echo $var;
?>
Personally, I would calculate the value before shooting it to your smarty template. Are you using a framework? If yes, you should have a controller and you should calculate it from there. You should always try to minimize the amount of code in templates
Note: I found the code here: http://css-tricks.com/snippets/php/time-ago-function/