note 114832 deleted from function.date by sobak
| From: | sobak@php.net | Date: | Thu, 01 Dec 2016 13:44:42 +0000 |
| Subject: | note 114832 deleted from function.date by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207849@lists.php.net to get a copy of this message | ||
Note Submitter: Manu Manjunath
----
If you want to use "u" format specifier for micrseconds without changing to DateTime
object, you may write a function as below:
<?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);
}
?>
You can safely replace your date() function with date_with_micro().