View previous topic :: View next topic |
Author |
Message |
jarofgreen Smarty Rookie
Joined: 09 Jul 2010 Posts: 14
|
Posted: Tue Oct 26, 2010 10:48 am Post subject: Convert number of seconds to human readable (NOT date!) |
|
|
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 |
|
Back to top |
|
invliD Smarty Rookie
Joined: 03 Mar 2011 Posts: 5
|
Posted: Mon May 02, 2011 1:39 pm Post subject: Re: Convert number of seconds to human readable (NOT date!) |
|
|
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 |
|
jarofgreen Smarty Rookie
Joined: 09 Jul 2010 Posts: 14
|
Posted: Mon Jun 06, 2011 3:21 pm Post subject: |
|
|
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 |
|
Onedamobamomi Smarty n00b
Joined: 08 Jul 2011 Posts: 4 Location: Greenland
|
Posted: Tue Oct 18, 2011 12:10 am Post subject: Convert number of seconds to human readable NOT date |
|
|
actually they shouldn't have to remove the folder. Just use the update button for the plugin. |
|
Back to top |
|
jarofgreen Smarty Rookie
Joined: 09 Jul 2010 Posts: 14
|
Posted: Tue Oct 18, 2011 7:24 am Post subject: Re: Convert number of seconds to human readable NOT date |
|
|
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 |
|
charrison1988 Smarty n00b
Joined: 08 Nov 2011 Posts: 2
|
Posted: Tue Nov 08, 2011 3:14 am Post subject: |
|
|
I don't know how to use this code. How is it installed into Smarty? |
|
Back to top |
|
jarofgreen Smarty Rookie
Joined: 09 Jul 2010 Posts: 14
|
Posted: Tue Nov 08, 2011 10:00 am Post subject: |
|
|
Simply put it in a file called "modifier.seconds_to_words.php" and stick in in your smarty/plugins folder. |
|
Back to top |
|
charrison1988 Smarty n00b
Joined: 08 Nov 2011 Posts: 2
|
Posted: Tue Nov 08, 2011 10:01 am Post subject: |
|
|
Thanks! Works perfectly. |
|
Back to top |
|
|