Created
August 16, 2013 10:26
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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