|
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 |
stot Smarty Rookie
Joined: 13 Nov 2006 Posts: 17
|
Posted: Tue Jun 30, 2009 12:36 am Post subject: date modefier like in youtube, e.g. '1 hour ago' |
|
|
I searched out for something that does this date formatting stuff (like at youtube comments) and convert a timestamp or datetime into spoken language. I could not find anything that satisfied me, so I sat down and cranked it out.
just three short remarks:
1) the code currently creates german words, but this could be changed easily using the array at top of the function. maybe somebody wants to make it configurable using an parameter.
2) the code works well with unix timestamps generated using time() and also seamless with mysql datetime strings like '2009-06-28 02:22:46'
3) the debug and echo stuff could be ignored or deleted
here is the modifier function, just copy it in a file called modifier.timeAgo.php and place the file in the smarty plugin directory.
hope you enjoy it!
Code: |
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty date modifier plugin
* Purpose: converts unix timestamps or datetime strings to words
* Type: modifier<br>
* Name: timeAgo<br>
* @author Stephan Otto
* @param string
* @return string
*/
function smarty_modifier_timeAgo( $date)
{
// for using it with preceding 'vor' index
$timeStrings = array( 'kurzem', // 0 <- now or future posts :-)
'Sekunde', 'Sekunden', // 1,1
'Minute','Minuten', // 3,3
'Stunde', 'Stunden', // 5,5
'Tag', 'Tagen', // 7,7
'Woche', 'Wochen', // 9,9
'Monat', 'Monaten', // 11,12
'Jahr','Jahren'); // 13,14
$debug = false;
$sec = time() - (( strtotime($date)) ? strtotime($date) : $date);
if ( $sec <= 0) return $timeStrings[0];
if ( $sec < 2) return $sec." ".$timeStrings[1];
if ( $sec < 60) return $sec." ".$timeStrings[2];
$min = $sec / 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;
echo ($debug == true) ? "hours: ".floor($hrs+0.5)."<br />" : '';
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;
echo ($debug == true) ? "days: ".floor($days+0.5)."<br />" : '';
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;
echo ($debug == true) ? "weeks: ".floor($weeks+0.5)."<br />" : '';
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];
}
?>
|
to use it, in your template:
Code: | (vor {$myDate|timeAgo}) |
the date must be assigned to $myDate and the output will be e.g.
- (vor 7 Stunden)
- (vor 2 Tagen) |
|
Back to top |
|
atl Smarty Rookie
Joined: 17 May 2009 Posts: 14
|
Posted: Sun Sep 27, 2009 3:26 am Post subject: trouble Implementing... Some Help Please? |
|
|
Alright, I am totally new to Smarty but I can understand new stuff.
I added this modifier.timeAgo.php to the plugins folder like instructed, I altered my TABLE to add the time stamp like so:
Code: | ALTER TABLE mytable ADD dt_added TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP |
I added some stuff, the new TIMESTAMP displayed perfectly.
eg; each row in dt_added
2009-09-26 19:50:19
2009-09-26 19:50:32
2009-09-26 19:50:35
Now when I call it from my template with: {$dt_added|timeAgo}
It displays the hour from the 1st hour.
Like, 1 new entry now displays '20 Hours Ago' then 1 new entry an hour later displays '21 Hours Ago' so it's not converting the time, just from the date.
Am I doing something wrong here? I have been fidgeting with it for hours! Thanks! |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
|
Back to top |
|
stot Smarty Rookie
Joined: 13 Nov 2006 Posts: 17
|
Posted: Sun Nov 15, 2009 10:03 am Post subject: Re: trouble Implementing... Some Help Please? |
|
|
atl wrote: |
I added some stuff, the new TIMESTAMP displayed perfectly.
eg; each row in dt_added
2009-09-26 19:50:19
2009-09-26 19:50:32
2009-09-26 19:50:35
Now when I call it from my template with: {$dt_added|timeAgo}
It displays the hour from the 1st hour.
Like, 1 new entry now displays '20 Hours Ago' then 1 new entry an hour later displays '21 Hours Ago' so it's not converting the time, just from the date.
Am I doing something wrong here? I have been fidgeting with it for hours! Thanks! |
Hi atl,
are you sure, that your variable $dt_added contains the correct timestamps?
I tried your format and it worked correctly.
Code: | {assign var='myDate' value='2009-09-26 19:50:19'}
vor {$myDate|timeAgo} |
output: vor 2 Monaten
The timeAgo modifier displays the distance since the start of the unix timestamps (e.g. 1970) if no correct timestamp is given:
Code: | {assign var='myDate' value='something wrong'}
{$myDate|timeAgo} |
output: vor 41 Jahren
hope this helps you out |
|
Back to top |
|
stot Smarty Rookie
Joined: 13 Nov 2006 Posts: 17
|
Posted: Sun Nov 15, 2009 10:08 am Post subject: localization |
|
|
ah and the timestrings array should be localized into your language of course. The code at the top is displaying german.
best regards
Stephan |
|
Back to top |
|
atl Smarty Rookie
Joined: 17 May 2009 Posts: 14
|
Posted: Sat Jan 02, 2010 6:50 am Post subject: |
|
|
I figured out why this is not working for me. The timestamps I am using only display date eg; 2009-09-26 00:00:00 and not date and hr:min:sec eg; 2009-09-26 19:50:19
Script works fine, thanks everyone. |
|
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
|
|