| 0 comments ]


<?php

// function to calculate the elapsed time from microtime

function elapsedTime($startTime = '', $endTime = '',$format="SECONDS",$decimals = 4)
    {
       
        if (!$startTime)
        {
            return '';
        }

        if (!$endTime)
        {
            $endTime = microtime();
        }
   
        list($startMicroSecond, $startSecond) = explode(' ', $startTime);
        list($endMicroSecond, $endSecond) = explode(' ', $endTime);
       
        //number_format(($em + $es) - ($sm + $ss), $decimals);
       
        if($format){
            $format = strtoupper($format);
        }
        if($format == 'SECONDS'){       
            $timeVal  =  number_format((($endMicroSecond + $endSecond) - ($startMicroSecond + $startSecond)),$decimals)." SECONDS";
        }
        else if($format == 'MINUTS'){
            $timeVal  =  number_format((($endMicroSecond + $endSecond) - ($startMicroSecond + $startSecond))/60 ,$decimals). " MINUTS";
        }       
        else if($format == 'HOURS'){
            $timeVal  =  number_format((($endMicroSecond + $endSecond) - ($startMicroSecond + $startSecond))/3600,$decimals). " HOURS";
        }
        else if($format == 'DAYS'){       
            $timeVal  =  number_format(((($endMicroSecond + $endSecond) - ($startMicroSecond + $startSecond))/3600)/24,$decimals)." DAYS";
        }       
       
        return $timeVal;       
    }
   
   
    // How to use ....
   
    $a = "0.85969200 1242372611";
    $b = microtime();
   
   
    // examples    
   
    echo '<p>Elapsed Time  in Days: '.elapsedTime($a,$b,"DAYS",2).'</p>';
    echo '<p>Elapsed Time  in Hours: '.elapsedTime($a,$b,"HOURS",2).'</p>';
    echo '<p>Elapsed Time  in Minuts: '.elapsedTime($a,$b,"MINUTS",2).'</p>';
    echo '<p>Elapsed Time  in Seconds: '.elapsedTime($a,$b,"SECONDS",2).'</p>';
   
    // only starting time
   
    echo '<p>Elapsed Time  in Hours: '.elapsedTime($a,'',$format="HOURS",6).'</p>';
   
    // start time only with no format and no decimel positions
   
    echo '<p>Elapsed Time  in Seconds: '.elapsedTime($a).'</p>';
   
   
?>


enjoy PHPing

0 comments

Post a Comment

Please put your comments here. your questions, your suggestions, also what went wrong with me.