split decimal

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

split decimal

Post 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).
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

Re: split decimal

Post 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];
phu
Forum Commoner
Posts: 61
Joined: Tue Mar 30, 2010 6:18 pm

Re: split decimal

Post 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. :)
Post Reply