Page 1 of 1

split decimal

Posted: Fri Apr 23, 2010 10:57 am
by dimxasnewfrozen
I have the following code:

Code: Select all

$mins = 75;
$mins = $mins / 60;  // returns 1.25
list($hrs, $mins) = split(".", $mins, 2); //I want to return 1 and .25 separately 
$mins = $mins * .60; // should return 15 
I need a way to split the decimal so that I can get the two parts but I'm not returning the first part (which in this case is a 1).

Re: split decimal

Posted: Fri Apr 23, 2010 11:01 am
by dimxasnewfrozen
Well... this always seem to happen... as soon as I submit the post, I found a solution:

Code: Select all

		
$mins = $minutes / 60;
$time = explode(".", $mins);
echo $time[0];
echo $time[1];

Re: split decimal

Posted: Fri Apr 23, 2010 11:59 am
by phu
You could also use...

Code: Select all

$time = $mins - (int)$mins;
Unlike explode(), it will give you a numeric type. Not all that necessary with PHP, but still good practice. :)