note 114832 added to function.date
| From: | ManuManjunath | Date: | Fri, 11 Apr 2014 11:59:11 +0000 |
| Subject: | note 114832 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199247@lists.php.net to get a copy of this message | ||
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().
----
Server IP: 103.21.58.61
Probable Submitter: 202.83.18.29
----
Manual Page -- http://php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/114832
Del: integrated -- https://master.php.net/note/delete/114832/integrated
Del: useless -- https://master.php.net/note/delete/114832/useless
Del: bad code -- https://master.php.net/note/delete/114832/bad+code
Del: spam -- https://master.php.net/note/delete/114832/spam
Del: non-english -- https://master.php.net/note/delete/114832/non-english
Del: in docs -- https://master.php.net/note/delete/114832/in+docs
Del: other reasons-- https://master.php.net/note/delete/114832
Reject -- https://master.php.net/note/reject/114832
Search -- https://master.php.net/manage/user-notes.php