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

how to convert numbers into hour:minutes

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


Joined: 05 Mar 2014
Posts: 8

PostPosted: Fri Mar 07, 2014 2:04 pm    Post subject: how to convert numbers into hour:minutes Reply with quote

currently using this string

{math equation="(( x ) / z )" x=$single_array[i].TIME_SPENT z=60}

value of x is 90, this outputs 1.5 which is great, how can I get it to display as 1h:30m ?

Thanks everyone
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Mar 07, 2014 3:05 pm    Post subject: Reply with quote

Assign the value to a template variable:

{math equation="(( x ) / z )" x=$single_array[i].TIME_SPENT z=60 assign="time"}

Then feed $time into a custom function that takes the number and translates it into a time format. You will need to write that function, or find one that has been written.
Back to top
View user's profile Send private message Visit poster's website
anandlall12
Smarty Rookie


Joined: 05 Mar 2014
Posts: 8

PostPosted: Fri Mar 07, 2014 10:01 pm    Post subject: Reply with quote

mohrt wrote:
Assign the value to a template variable:

{math equation="(( x ) / z )" x=$single_array[i].TIME_SPENT z=60 assign="time"}

Then feed $time into a custom function that takes the number and translates it into a time format. You will need to write that function, or find one that has been written.


Thanks for your reply. I searched the forum but I did not find anything Sad

I also found This online and made a few modifications to it.

{php}
$seconds = 90*60
$hours = floor($seconds / 3600);
$mins = floor(($seconds - ($hours*3600)) / 60);
$secs = floor($seconds % 60);
{/php}

Then I echo using {php}{ echo $hours.'h:'. $mins.'m';}{/php} and it does display the way I want it to. BUT do you know how I can assign {$string} which gets value from MySQL to $seconds = {string /60}

Thanks for the help


Last edited by anandlall12 on Fri Mar 07, 2014 10:05 pm; edited 1 time in total
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Mar 07, 2014 10:04 pm    Post subject: Reply with quote

There are a whole bunch of ideas here.

http://stackoverflow.com/questions/3172332/convert-seconds-to-hourminutesecond
Back to top
View user's profile Send private message Visit poster's website
anandlall12
Smarty Rookie


Joined: 05 Mar 2014
Posts: 8

PostPosted: Fri Mar 07, 2014 10:10 pm    Post subject: Reply with quote

mohrt wrote:
There are a whole bunch of ideas here.

http://stackoverflow.com/questions/3172332/convert-seconds-to-hourminutesecond


Those are very similar to the one I have above, and they do work, just that I need it to use values from mysql table rather than a static value.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Mar 07, 2014 10:19 pm    Post subject: Reply with quote

If you assign your value to the $time template var, you can do this inside the php tags.

$this->getTemplateVars('time')

However, php tags are bad practice, use smarty plugins. see here:

http://www.smarty.net/best_practices
Back to top
View user's profile Send private message Visit poster's website
anandlall12
Smarty Rookie


Joined: 05 Mar 2014
Posts: 8

PostPosted: Fri Mar 07, 2014 10:38 pm    Post subject: Reply with quote

mohrt wrote:
If you assign your value to the $time template var, you can do this inside the php tags.

$this->getTemplateVars('time')

However, php tags are bad practice, use smarty plugins. see here:

http://www.smarty.net/best_practices


I am very new to smarty so that might be an issue for me to code, However I was reading some of the post here and I an thinking of doing something like this

{math equation="(( x ) * z / a )" x=$single_array[i].TIME_SPENT z=60 a=3600} which displays 1.5 as x=90 how can remove the decimal from this string? if I could get that to work then all I have to do is to * the result by 60 and I will get my minutes Smile

Thanks once again
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Mar 07, 2014 11:12 pm    Post subject: Reply with quote

use round() floor() ceil() in your math equation?
Back to top
View user's profile Send private message Visit poster's website
anandlall12
Smarty Rookie


Joined: 05 Mar 2014
Posts: 8

PostPosted: Fri Mar 07, 2014 11:23 pm    Post subject: Reply with quote

mohrt wrote:
use round() floor() ceil() in your math equation?


I actually used format="%.b" and it removed the decimal and display 1 but having other issues with minutes because I need to take the value from equation 1 and *3600 /60
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 -> General 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