Sidebar

startup_tutorial_and_checklist:design_configuration:smarty_template_code_extensions:time_since_start

Time Since Start In Listing Details

Smarty is very versatile in what you can do. Within this article we introduce the concept of creating your own smarty plugin to extend functionality even further. Many times within a smarty template there isn't the correct display or simple functionality necessary for your needs. Since the smarty engine does not allow php directly in a smarty template you can use a smarty plugin to modify data already within a smarty template with needing to modify the base php code to get it. So you get the benefit of using php to get a slightly different bit of data or display without needing to go all the way to create an addon OR modify the base php code. And always remember that any base php code modifications require you to reinsert those changes after each Geo software version update. Adding your own smarty plugin does not change base code and is not disturbed by future Geo version updates….well in the odd case that we create a smarty plugin with the exact same name as your custom one.

The feature looked to create here is a "time since" display where a date is passed into the smarty plugin. This will take a date like in the "date started" value available in the listing details template:

{$date_started}

That tag displays the date that listing started. The date format it displays in uses the following configuration within the admin tool:

LISTING SETUP > FIELDS TO USE > General Date/Time Display Format

Only specific date/time displays set in the above setting will work. You must choose one that is a "standard php time format" for that function to return a proper date. We tested with a format like:

27 Feb 2015, 1:25

and worked but will not with many of the time formats available in that dropdown of choices. Set the format you want and try the tag within your listing details page or where ever you have a timestamp showing.

Creating the Smarty Plugin

You take the following code:

<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */
 
/**
 * Smarty relative date / time since passed in date plugin
 *
 * @version 1.0
 * @param string
 * @return string
 */
function smarty_modifier_date_since_start($date)
{
	if(!$date){
		return 'N/A';
	}
	
	$timeStrings = array('now','sec','secs','min','mins','hour','hours','day','days','wk','wks','mth','mths','yr','yrs');
	$starting_time = (int)strtotime($date);
 	$difference = time() - $starting_time; 
	if ( $difference <= 0) return $timeStrings[0]; 
       
	if ( $difference < 2) return $difference." ".$timeStrings[1]; 
	if ( $difference < 60) return $difference." ".$timeStrings[2]; 
       
	$min = $difference / 60; 
	if ( floor($min+0.5) < 2) return floor($min+0.5)." ".$timeStrings[3]; 
	if ( $min < 60) return floor($min+0.5)." ".$timeStrings[4]; 
       
	$hrs = $min / 60; 
	if ( floor($hrs+0.5) < 2) return floor($hrs+0.5)." ".$timeStrings[5]; 
	if ( $hrs < 24) return floor($hrs+0.5)." ".$timeStrings[6]; 
       
	$days = $hrs / 24; 
	if ( floor($days+0.5) < 2) return floor($days+0.5)." ".$timeStrings[7]; 
	if ( $days < 7) return floor($days+0.5)." ".$timeStrings[8]; 
       
	$weeks = $days / 7; 
	if ( floor($weeks+0.5) < 2) return floor($weeks+0.5)." ".$timeStrings[9]; 
	if ( $weeks < 4) return floor($weeks+0.5)." ".$timeStrings[10]; 
       
	$months = $weeks / 4; 
	if ( floor($months+0.5) < 2) return floor($months+0.5)." ".$timeStrings[11]; 
	if ( $months < 12) return floor($months+0.5)." ".$timeStrings[12]; 
       
	$years = $weeks / 51; 
	if ( floor($years+0.5) < 2) return floor($years+0.5)." ".$timeStrings[13]; 
	return floor($years+0.5)." ".$timeStrings[14]; 

}

and paste it into a file named:

modifier.date_since_start.php

and insert that file into the following directory of your Geo installation:

/classes/geo_smarty_plugins/

You can make changes in that file to translate English to another language or make it return some other output in some other way.

Usage Within Smarty Tag

Listing Details Page

To use within the following tag available for use in the listing details template (by default listing_auction.tpl or listing_classified.tpl):

{$date_started}

You would use the following:

{$date_started|date_since_start}

That works for that tag. But note that modifier is now available in any smarty template and within any tag. So have fun. Place that modifier in other "time date display" tags, pass in a format that will work and check the display.

Category Browsing Page

The following templates would be in use within the category browsing feature:

/main_page/browsing_sub/featured_gallery.tpl
/main_page/browsing_sub/gallery.tpl
/main_page/browsing_sub/grid.tpl
/main_page/browsing_sub/list.tpl
/main_page/browsing_sub/show_all_fields/gallery.tpl
/main_page/browsing_sub/show_all_fields/list.tpl

Within those you'll find this "date based" tags:

{$listing.entry_date}

So try something like:

{$listing.entry_date|date_since_start}
startup_tutorial_and_checklist/design_configuration/smarty_template_code_extensions/time_since_start.txt · Last modified: 2015/02/27 14:43 by geojames