Just curious how to convert microtime() to milliseconds and/or seconds... (not for purposes of timing a script execution)

Also, will microtime() work accurately on a Windows server as well as Unix? I've read this returns the Unix timestamp... (just curious for compatibility reasons later down the road)

    Try this

    $secs = microtime(); 
    echo number_format($secs,3); 
    print (" seconds."); 
    

    to give you time in seconds for a page load

      on second thought... that's not what I need...

      I need to know how to record the current date/time down to the second... and the be able to "subtract" or compare these dates easily... any way you can think of?

        <?php

        // =================================
        // This class is for the timer o_0
        // =================================

        $Timer = new Timer;

        Class Timer{

        function startTimer()
        {
        	global $starttime;
        	$mtime = microtime();
        	$mtime = explode(' ', $mtime);
        	$mtime = $mtime[1] + $mtime[0];
        	$starttime = $mtime;
        }
        
        
        function endTimer()
        {
        	global $starttime;
        	$mtime = microtime();
        	$mtime = explode(' ', $mtime);
        	$mtime = $mtime[1] + $mtime[0];
        	$endtime = $mtime;
        	$totaltime = round(($endtime - $starttime), 5);
        	return $totaltime;
        }

        }

        ?>

          I'm sorry... thank you for the help... let me be more specific... I record a date/time on one page of a script... then on the next page, I get the current date/time and compare it to the date/time that was passed from the previous page.

          If it is greater than, say, a 5-minute difference, the first page is reloaded with a note asking the user to re-verify the current information is acceptable, then submit the information again within 5 minutes.

          It doesn't seem hard... make sure the current date is the same as it was before and then compare the time... BUT, what about those orders that take place just before midnight... and then what about the orders that take place just before midnight where the next day is a new month...

          any suggestions besides just make them submit the form twice in this situation?

            Can't you just use the date function?

            On page 1.

            $loadTimeStamp = date("U");


            Pass that to the next page.

            On Page 2.

            $currentTimeStamp = date("U");
            $fiveMinutes = 300;// 300 seconds in 5 minutes
            if (($currentTimeStamp - $loadTimeStamp) > $fiveMinutes) {
            echo "reverify information";
            }

            This will work on weekends, at midnight, leapyear, etc since it is dealing the the number of seconds since the Unix Epoch (BTW, this works on windows servers as well).

              thanks a lot... I actually ended up getting the same result with:

              $secs = explode(' ', microtime());

              print($secs[1]);

              However, I think your way may be more efficient/cleaner... thanks again.

                Write a Reply...