note 65446 deleted from function.microtime by tularis
| From: | tularis@php.net | Date: | Sun, 22 Jun 2008 12:10:17 +0000 |
| Subject: | note 65446 deleted from function.microtime by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-140683@lists.php.net to get a copy of this message | ||
Note Submitter: Eric Pecoraro
----
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";
}
?>