Generating Unique Microseconds Granular Timestamps with PHP
October 20, 2014

The time() function in PHP returns the current Unix timestamp; which is the time measured in the number of seconds since the Unix Epoch. Very useful in certain cases, but not always. This function, like most other PHP functions, is also cross platform compatible as it works on Unix, Linux, Windows, and Mac.

PHP's microtime() function is more accurate and granular as it returns the current Unix timestamp in microseconds. Our problem with it is that it returns a string containing spaces and dots and that isn't useful if, for example, you're generating part of a filename or an HTML or CSS identifier from within PHP.

This is why we've written this little, yet super useful, function below. It's based on PHP's microtime() function but returns a clean, digits only, string of characters.

Enjoy!

 


<?php

/**
 * Generates and returns a string of digits representing the time of the
 * current system in microseconds granularity. 
 * 
 * Compared to the standard time() function, the microtime() function is more 
 * accurate and in addition, successive quick calls inside a loop generate 
 * unique results; which can be quite useful in certain cases. 
 * 
 * Our function below generates digits only output based on the time stamp
 * generated by the microtime() function.
 * 
 * @return string
 */
function get_clean_microtimestamp_string() {
    //Get raw microtime (with spaces and dots and digits)
    $mt = microtime();
    
    //Remove all non-digit (or non-integer) characters
    $r = "";
    $length = strlen($mt);
    for($i = 0; $i < $length; $i++) {
        if(ctype_digit($mt[$i])) {
            $r .= $mt[$i];
        }
    }
    
    //Return
    return $r;
}

 

Note that microtime() is only available on operating systems that support the gettimeofday() system call. We tested it on Windows 7, Windows 8, and on Ubuntu 14 and it works perfectly well on all of them.

Note also that microtime() generates a different output value even when called successively multiple times; which is more than useful. Try it and see for yourself. Alternatively, use the PHP code below from the command line to see how we tested the function on our end.

 


<?php

function array_has_duplicates($array) {
   return count($array) !== count(array_unique($array));
}

$microtimes = array();
for($i = 0; $i < 1000; $i++) {
    $microtimes[] = get_clean_microtimestamp_string();
}

foreach($microtimes as $microtime) {
    echo $microtime . "\n";
}

if(array_has_duplicates($microtimes)) {
    echo "FOUND DUPLICATES!\n";
}
else {
    echo "NO DUPLICATES FOUND. AWESOME!\n";
}


Development PHP Open Source CLI

Share this post


Written by
Mario Awad

Founder of SOFTKUBE, lead developer, and getting things done addict. Passionate about open source, user interface design, business development, and the tech world.

More about Mario Awad


About
SOFTKUBE

A small team of experts developing simple, usable, and high-quality web solutions. We blog about business, entrepreneurship, web development, and technology.

More about us


Recent Posts

Custom Theme Migration from Drupal 9 to Drupal 10

How to Change the Most Recent Git Commit Message

How to Make Google Chrome Forget a Permanent HTTP 301 Redirect

Finding and Fixing Unintended Body Overflow to Remove Horizontal Scrollbars

View all posts


All Posts Categories

Business Cheat Sheets CLI Design Development Downloads Drupal Email Google Apps HID Keyboards Multilingualism Open Source Philosophy PHP Pointing Devices Productivity Quotes Science Security SEO Technology Thoughts Windows Zend Framework