Made of Everything You're Not

Because there's too much info for my brain.
  • Home
  • Projects
  • Portfolio
  • Resume
« Starting to Develop For Blackberries
For your Perspective: My Email Hell »

Smarty Relative Datetime Modifier

In Extending the Smarty Template Engine I outlined the steps needed to create all the varieties of plugins for the Smarty template engine. Continuing with that tutorial, and to provide a little context, here’s a quick Smarty plugin for converting timestamps into relative timestamps.

Smarty Logo

Smarty Logo

This is a Smarty module plugin; notice how the name conforms to the naming conventions with the prefix “smarty_modifier_”

<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */
 
/**
 * Smarty relative date / time plugin
 *
 * Type:     modifier<br>
 * Name:     relative_datetime<br>
 * Date:     March 18, 2009
 * Purpose:  converts a date to a relative time
 * Input:    date to format
 * Example:  {$datetime|relative_datetime}
 * @author   Eric Lamb <eric@ericlamb.net>
 * @version 1.0
 * @param string
 * @return string
 */
function smarty_modifier_relative_datetime($timestamp)
{
	if(!$timestamp){
		return 'N/A';
	}
 
	$timestamp = (int)strtotime($timestamp);
	$difference = time() - $timestamp;
	$periods = array("sec", "min", "hour", "day", "week","month", "year", "decade");
	$lengths = array("60","60","24","7","4.35","12","10");
	$total_lengths = count($lengths);
 
	if ($difference > 0) { // this was in the past
		$ending = "ago";
	} else { // this was in the future
		$difference = -$difference;
		$ending = " from now";
	}
	//return;
 
	for($j = 0; $difference > $lengths[$j] && $total_lengths > $j; $j++) {
		$difference /= $lengths[$j];
	}
 
	$difference = round($difference);
	if($difference != 1) {
		$periods[$j].= "s";
	}
 
	$text = "$difference $periods[$j] $ending";
 
	return $text;
}

Installation

Just copy the contents above and paste into a file called:
modifier.relative_datetime.php

Place the file in your Smarty plugin directory and you’re good to go.

Usage Example:

{$item.timestamp|relative_datetime}
Bookmark and Share

Related Posts

WP-Click-Track 0.7.2 Released
ExpressionEngine White Screen Fix
Portability Is A Good Goal
Create Expression Engine Plugin
hResume hKit Profile

Tags: php, smarty

This entry was written by Eric Lamb and posted on Tuesday, March 24th, 2009 at 9:51 am and is filed under Code, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Click here to cancel reply.

  • Subscribe: Entries | Comments
  • About Me

    Email Email
    Twitter Twitter
    310.739.3322
  • Categories

    • Brain Dump
    • Business
    • Code
    • IT
    • Programming
    • Rant
    • Servers
  • Archives

    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008

Copyright © 2008 - 2010 Eric Lamb - All rights reserved