Re: strtotime() lost precision?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Mario de Frutos Dieguez wrote:
Hi!

I'm back hahaha :D

I'm making functions to calculate the duration (only laboral days) between 2 dates and the initial and final date between a duration give me in laboral days.

I use the following function to make the diference between 2 dates:

function fecDiferenciaFechas($fecFechaInicio,$fecFechaFin)
       {
           $fecFechaInicio=strtotime($fecFechaInicio);
           $fecFechaFin=strtotime($fecFechaFin);
                     if ($fecFechaFin == -1 || $fecFechaInicio == -1)
           {
               return false;
           }
                     $iDiferencia = $fecFechaFin - $fecFechaInicio;
                     //Inicializamos la variable
           $iDias = 0;
                     //Devolvemos la diferencia en dias
           return ($iDiferencia/86400);
       }

The problem is that with a duration of 220 the return is 219.958333, where im losing precision?

computers can't stores floats precisely, precision errors are. Rasmus gave a good explaination of this on this list not so long ago - that said there is lots of info on this on the web, here is one good article to get you started:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

on a more practical note take a look at the following php functions:

round(), ceil(), floor()

one of those should provide you with a 'fix' to the precision error(s) you are
seeing.


(Sorry for my home english :P)

Don't worry about it! my spanish begins and ends at 'Hasta la vista' ;-) actually that's not strictly true - I managed to have a whole conversation in Spanish once only using the word 'bale' (I think there is supposed to be an accent on the 'e' of 'bale')

(btw. I couldn't quite figure out what 'home english' is)



-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux