|
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 |
ngardner Smarty n00b
Joined: 13 Oct 2010 Posts: 2
|
Posted: Wed Oct 13, 2010 6:53 pm Post subject: My modifier that converts date/time to human friendly phrase |
|
|
I'm unable to post links since this is my first post, so I added it to the smarty plugin wiki page.
Its a modifier plugin which converts a date into something human friendly such as '2 minutes ago', 'Tuesday at 3:52pm', or 'March 12 at 6:49am' just like Facebook does. Works great for comments and message boards!
Its added to the Smarty Plugin wiki pages as 'timeSpan'
Please let me know what you think. |
|
Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
|
Back to top |
|
ngardner Smarty n00b
Joined: 13 Oct 2010 Posts: 2
|
Posted: Thu Oct 14, 2010 3:02 pm Post subject: |
|
|
yes, similar to that, but I think mine is easier to modify so you can add/modify your own phrases.
Heres the code
Code: |
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty time span plugin
*
* Type: modifier<br>
* Name: timeSpan<br>
* Purpose: convert date to a human friendly string such as "32 minutes ago"
* @link:
* @author Nathan Gardner <nathan at factory8 dot com>
* @param string
* @return string
*/
function smarty_modifier_timeSpan($string)
{
$timestamp = strtotime($string);
$now = time();
$timeSpan = $now-$timestamp;
$date = date("m/d/y",$timestamp);
$dateNow = date("m/d/y",$now);
$dayOfMonth = date("l",$timestamp);
$time = date("g:ia",$timestamp);
$minutes = round($timeSpan/60);
$hours = round($timeSpan/3600);
$month = date("F",$timestamp);
$dayOfMonthNumeric = date("d",$timestamp);
$year = date("Y",$timestamp);
$yearNow = date("Y",$now);
if($timeSpan < 180) { // less than 3 minutes
$spanMessage = 'Just now';
} else if ($timeSpan < 3000) { // less than 50 minutes ago
$spanMessage = $minutes . ' minutes ago';
} else if ($timeSpan < 86400) { // less than 24 hours ago
$spanMessage = $hours . ' hours ago';
} else if ($date == $dateNow) { // yesterday
$spanMessage = 'Yesterday at ' . $time;
} else if ($timeSpan < 432000) { // less than 5 days ago
$spanMessage = $dayOfMonth . ' at ' . $time;
} else if ($year == $yearNow) { // more than 5 days ago this year
$spanMessage = $month . ' ' . $dayOfMonthNumeric . ' at '. $time;
} else { // more than a year ago
$spanMessage = $month . ' ' . $dayOfMonthNumeric . ' ' . $year . ' at ' . $time;
}
return $spanMessage;
}
?>
|
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Oct 14, 2010 3:07 pm Post subject: |
|
|
What if the string passed in is a timestamp already, or not parsible by strtotime()? What if the date is in the future? You might at least do a test and return the string unchanged if there is a problem.
Code: | $timestamp = strtotime($string);
if(empty($timestamp))
return $string; |
|
|
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
|