Skip to content

Instantly share code, notes, and snippets.

@m-manu
Created August 16, 2013 10:26
Show Gist options
  • Save m-manu/6248802 to your computer and use it in GitHub Desktop.
Save m-manu/6248802 to your computer and use it in GitHub Desktop.
Quick replacement to PHP's date() function to handle the 'u' format specifier (for microseconds)
<?php
/**
* Quick replacement to date() function to handle the 'u' format specifier (for microseconds)
* @param string $format Date format string - the same format string you would pass to date() function
* @param float $timestamp [optional] Unix timestamp with microseconds - Typically output of <b>microtime(true)</b>
* @return string Formatted string
*/
function date_with_micro($format, $timestamp = null) {
if (is_null($timestamp) || $timestamp === false) {
$timestamp = microtime(true);
}
$timestamp_int = (int) floor($timestamp);
$microseconds = (int) round(($timestamp - floor($timestamp)) * 1000000.0, 0);
$format_with_micro = str_replace("u", $microseconds, $format);
return date($format_with_micro, $timestamp_int);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment