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

Convert number of seconds to human readable (NOT date!)

 
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
jarofgreen
Smarty Rookie


Joined: 09 Jul 2010
Posts: 14

PostPosted: Tue Oct 26, 2010 10:48 am    Post subject: Convert number of seconds to human readable (NOT date!) Reply with quote

This converts an integer into a string.

60 => "1 Minute"
72 => "1 Minute 12 Seconds"

View the latest version at
http://elastik.svn.sourceforge.net/viewvc/elastik/trunk/includes/smarty/plugins/modifier.seconds_to_words.php?view=markup

I also claim a cookie: Like a good programmer, I did PHPUnit tests:
http://elastik.svn.sourceforge.net/viewvc/elastik/trunk/includes/tests/SmartySecondsToWordsTest.php?view=markup

Here is the code for convenience:

Code:

/**
 * @copyright 2010 MAPIX Technologies Ltd, UK, http://mapix.com/
 * @license http://en.wikipedia.org/wiki/BSD_licenses  BSD License
 * @package Smarty
 * @subpackage PluginsModifier
 */


function smarty_modifier_seconds_to_words($seconds) {
   if ($seconds < 0) throw new Exception("Can't do negative numbers!");
   if ($seconds == 0) return "zero seconds";

   $hours = intval($seconds/pow(60,2));
   $minutes = intval($seconds/60)%60;
   $seconds = $seconds%60;
   $out = "";
   if ($hours > 0) $out .= $hours . " hour". ($hours > 1 ? "s" : "")." ";
   if ($minutes > 0) $out .= $minutes . " minute". ($minutes > 1 ? "s" : "")." ";
   if ($seconds > 0) $out .= $seconds . " second". ($seconds > 1 ? "s" : "");
   return trim($out);
}



Feedback welcome Smile
Back to top
View user's profile Send private message
invliD
Smarty Rookie


Joined: 03 Mar 2011
Posts: 5

PostPosted: Mon May 02, 2011 1:39 pm    Post subject: Re: Convert number of seconds to human readable (NOT date!) Reply with quote

Hey.

This is a small fix for your plugin.
jarofgreen wrote:
Code:
($hours > 1 ? "s" : "")

You should rather use "$hours != 1" instead of > 1, because you also say 0 hours. Same for minutes and seconds.

invliD
Back to top
View user's profile Send private message
jarofgreen
Smarty Rookie


Joined: 09 Jul 2010
Posts: 14

PostPosted: Mon Jun 06, 2011 3:21 pm    Post subject: Reply with quote

Hello, sorry, I only just saw this.

The line your are referring to has a catch at the start for 0 hours.

Code:
if ($hours > 0) $out .= $hours . " hour". ($hours > 1 ? "s" : "")." ";


The reason for this is so that if its 37 mins and 24 secs ago, the output says "37 mins 24 secs" and not "0 hours 37 mins 24 secs". I think the former is better.

Given that your fix is not needed.

But if someone prefers "0 hours 37 mins 24 secs" they can remove the "if ($hours > 0)" check and apply your fix.

Thanks,
James
Back to top
View user's profile Send private message
Onedamobamomi
Smarty n00b


Joined: 08 Jul 2011
Posts: 4
Location: Greenland

PostPosted: Tue Oct 18, 2011 12:10 am    Post subject: Convert number of seconds to human readable NOT date Reply with quote

actually they shouldn't have to remove the folder. Just use the update button for the plugin.
Back to top
View user's profile Send private message Send e-mail
jarofgreen
Smarty Rookie


Joined: 09 Jul 2010
Posts: 14

PostPosted: Tue Oct 18, 2011 7:24 am    Post subject: Re: Convert number of seconds to human readable NOT date Reply with quote

Onedamobamomi wrote:
actually they shouldn't have to remove the folder. Just use the update button for the plugin.


I don't understand what you mean ... are you sure this is on the right thread?
Back to top
View user's profile Send private message
charrison1988
Smarty n00b


Joined: 08 Nov 2011
Posts: 2

PostPosted: Tue Nov 08, 2011 3:14 am    Post subject: Reply with quote

I don't know how to use this code. How is it installed into Smarty?
Back to top
View user's profile Send private message
jarofgreen
Smarty Rookie


Joined: 09 Jul 2010
Posts: 14

PostPosted: Tue Nov 08, 2011 10:00 am    Post subject: Reply with quote

Simply put it in a file called "modifier.seconds_to_words.php" and stick in in your smarty/plugins folder.
Back to top
View user's profile Send private message
charrison1988
Smarty n00b


Joined: 08 Nov 2011
Posts: 2

PostPosted: Tue Nov 08, 2011 10:01 am    Post subject: Reply with quote

Thanks! Works perfectly.
Back to top
View user's profile Send private message
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