Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#45193 closed defect (bug) (duplicate)

current_time() function will not return milliseconds / microseconds

Reported by: phillipburger Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Date/Time Keywords:
Focuses: Cc:


current_time() function in functions.php (https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/functions.php#L61) accepts a few formatting types and one is a timestamp format like "Y-m-d H:i:s"

The problem is there is no way to get milliseconds / microseconds like "Y-m-d H:i:s.u"because in current_time() it uses the PHP date() function and per PHP documentation:

Microseconds (added in PHP 5.2.2). Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds if DateTime was created with microseconds.


The way to get microseconds (this may not be the most clean, but gives an example) is like this:

$t = microtime(true);
$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new DateTime( date('Y-m-d H:i:s.'.$micro, $t) );
$d->format("Y-m-d H:i:s.u");

(notice the . before the u instead of : like other separators)

Is there a need to add some function similar to current_time() that will work with a DateTime object instead of the date() function to get more accurate information?

Note - this matters to me because I created a plugin with my own DB table where I am logging some interactions that happen fast enough they need to be logged at a low level. The standard MySQL Timestamp data type only goes to seconds, but with Timestamp(6) you can go all the way out to microseconds - and that is my need.

Thanks in advance!

Change History (2)

#1 @SergeyBiryukov
3 years ago

  • Component changed from General to Date/Time

#2 @Rarst
3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #40653.

Folding this into a general rewrite of the function.

Note: See TracTickets for help on using tickets.