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

Friendly dates

 
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 -> Plugins
View previous topic :: View next topic  
Author Message
mcdronkz
Smarty Rookie


Joined: 28 Feb 2006
Posts: 8

PostPosted: Mon Sep 04, 2006 1:38 pm    Post subject: Friendly dates Reply with quote

Hi, this is my friendly date plugin:

[php:1:6dc9d5a6c2]<?php
function smarty_modifier_friendlydatetime($timestamp) {
$curTimestamp = time();
$difference = $curTimestamp - $timestamp;

switch($difference) {
case ($difference <= 90):
return "1 minute ago";
break;
case ($difference <= 600):
//if the difference is less than 10 minutes
return round($difference / 60, -0.5)." minutes ago";
break;
case ($difference <= 900):
return "A quarter ago";
break;
case ($difference <= 1800):
return round($difference / 60, -0.5)." minutes ago";
break;
case ($difference <= 2700):
return "Half an hour ago";
break;
case ($difference <= 3600):
return "Three quarters ago";
break;
case ($difference <= 86400):
return floor($difference / 3600)." hour and ".floor(($difference - 3600) / 60)." minutes ago";
break;
default:
return date("\A\t d-m-Y \o\n H:i:s",$timestamp);
}
}
[/php:1:6dc9d5a6c2]

C&C Please, hope you like it.
Back to top
View user's profile Send private message
bluesman
Smarty n00b


Joined: 30 Nov 2003
Posts: 2
Location: Serbia

PostPosted: Fri Sep 15, 2006 12:43 pm    Post subject: Reply with quote

Hi, this is something I use in my scripts, it's a similar to the above, it just displays a bit different:

few seconds ago
4 minutes ago
3 hours ago
...
more than a month ago

[php:1:bd79ebb05c]
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: elapsed_time
* Author: Goran Pilipovic fka bluesman
* Purpose: print time pased since specific time
* Example: {elapsed_time timestamp=$ts}
* -------------------------------------------------------------
*/
function smarty_function_elapsed_time ($params, &$smarty)
{
extract($params);
if (empty($timestamp))
{
return '';
}
if (empty($names))
{
$names = "day, hour, minute, few seconds";
}

$n = explode (",",$names);
if (count($n) < 4)
{
$n = array ("day", "hour", "minute", "few seconds");
}

$difference = time() - intval($timestamp);

$days = floor($difference / (60 * 60 * 24));
$hours = floor($difference / (60 * 60));
$minutes = floor($difference / 60);

$s = "";
$val = 0;
if ($minutes > 0)
{
$val = $minutes;
$s = $n[2];
if ($hours > 0)
{
$val = $hours;
$s = $n[1];
if ($days > 0)
{
$val = $days;
$s = $n[0];
if ($days > 30)
{
$val = "more than a month";
$s = $n[0];
}
}
}
}
else
{
return $n[3];
}

$rest = $val % 10;
if ($s == $n[0])
{
$s = "day";
if ($rest > 1) { $s .= "s"; }
}

elseif ($s == $n[1])
{
$s = "hour";
if ($rest > 1) { $s .= "s"; }
}

elseif ($s == $n[2])
{
$s = "minute";
if ($rest > 1) { $s .= "s"; }
}

if (!empty($assign))
{
$smarty->assign($assign, $difference);
}

return "{$val} {$s} ago";
}

/* vim: set expandtab: */
?>
[/php:1:bd79ebb05c]
_________________
Best regards
Goran Pilipovic fka bluesman
Back to top
View user's profile Send private message Visit poster's website
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 -> Plugins 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