Reputation: 868
I have a variable $newTime
, which is time in seconds made by mktime()
, and I want to convert it into number of days.
In this case it is over 86.400
(which is a number of seconds in a day), and I try to divide it by 86.400
to get number of days (rounded).
But sometimes I get:
Case 1: 87951 / 86400 = 1.0179513888 (1 day ago)
Case 2: 156257 / 86400 = 1.8085300925 (2 days ago)
and sometimes:
Case 1: 87986 / 86400 = 1 (Should be 1 day ago)
Case 2: 156292 / 86400 = 1 (Should be 2 days ago)
This is the code:
$newTime = round(bcdiv($newTime, bcmul("24", bcmul("60", "60"))),0);
When I don't use bcmath, just the regular math ($newTime / (60*60*24)
), I get floating results every time.
Upvotes: 1
Views: 954
Reputation: 7192
Have you set the scale using bcscale
? If it is set to 0
, it could explain the behaviour. Please, try to call bcscale(100)
before your computation;
Upvotes: 3
Reputation: 140228
You could use:
$newTime = round( $newTime / (60 * 60 * 24));
With no bcmath at all please.
Examples:
$newTime = 156292;
$newTime = round( $newTime / (60 * 60 * 24));
echo $newTime; //2 Always
$newTime = 87986;
$newTime = round( $newTime / (60 * 60 * 24));
echo $newTime; //1 Always
Upvotes: 1