Hi

can any one pls help on the below thing

I am having a value as

91819.0

But I required the above data should be converted as time as below

9:18:19

Likewise it should be viceversa

Recommended Answers

cast as int to remove value after "."

then parse

Jump to Post

Try this

function setMyValue ($myValue) {
$newValue = (int)$myValue;
$myArray = array_reverse(str_split($newValue));
$myValue ="";
foreach ($myArray as $key=>$myDigits) {
    $myValue.=($key>0 && $key%2==0) ? ":" : "";
    $myValue.= $myDigits;
}
return strrev($myValue);
}

echo setMyValue(91819.0);
Jump to Post

All 5 Replies

cast as int to remove value after "."

then parse

Try this

function setMyValue ($myValue) {
$newValue = (int)$myValue;
$myArray = array_reverse(str_split($newValue));
$myValue ="";
foreach ($myArray as $key=>$myDigits) {
    $myValue.=($key>0 && $key%2==0) ? ":" : "";
    $myValue.= $myDigits;
}
return strrev($myValue);
}

echo setMyValue(91819.0);

Hi Bachov

Thanks for your code

Member Avatar

Casting as int will not round the float to an integer. Well, actually I suppose it rounds down like ceil(). It simply truncates.

If you want the decimal part to be considered, then I suggest you use the round() function to get your integer.

So:

$time = strrev(implode(":", str_split(strrev(round($myValue)), 2)));

heh!

Hi Bachov Varghese

Thanks.is working fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.