note 65446 added to function.microtime

From: Date: Mon, 01 May 2006 03:35:09 +0000
Subject: note 65446 added to function.microtime
Groups: php.notes 
Request: Send a blank email to php-notes+get-110232@lists.php.net to get a copy of this message
Even though this function uses gettimeofday(), it fits better here. I created it to provide unique IDs for image names that were being process in a fast loop scenario. <?php // 15 Digit Microtime Stamp (returns a 15 digit timestamp/unique id inside loops) // Inspired by Christian Wenz's excellent "PHP Phrasebook". function microtimestamp($id='') { $stamp = gettimeofday(); // id insures unique id in fast loops, no id is in sync w/ PHP's time() function if ($id=='id') { $divby=100000; } else { $divby=1000000; } $stamp = $stamp['sec'] + $stamp['usec'] / $divby ; $stamp = str_replace('.','',$stamp); $stamp = substr($stamp.'00000',0,15) ; // for consistent length return $stamp; } // TESTS // synced with time() function echo '<b>'.time()." <- time()</b><br>\n"; while ( $cnt < 11 ) { $cnt++; echo microtimestamp() . "<br>\n"; } // adds a few random seconds to time() ... but produces unique ids in fast loop echo "<b>Unique IDs</b><br>\n"; while ( $cnt2 < 11 ) { $cnt2++; echo microtimestamp('id') . "<br>\n"; } ?> ---- Server IP: 216.194.113.175 Probable Submitter: 71.245.182.66 ---- X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER, HTML_MESSAGE autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.microtime.php Edit -- http://master.php.net/note/edit/65446 Del: integrated -- http://master.php.net/note/delete/65446/integrated Del: useless -- http://master.php.net/note/delete/65446/useless Del: bad code -- http://master.php.net/note/delete/65446/bad+code Del: spam -- http://master.php.net/note/delete/65446/spam Del: non-english -- http://master.php.net/note/delete/65446/non-english Del: in docs -- http://master.php.net/note/delete/65446/in+docs Del: other reasons-- http://master.php.net/note/delete/65446 Reject -- http://master.php.net/note/reject/65446 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#110232) next »