[wp-trac] [WordPress Trac] #45193: current_time() function will not return milliseconds / microseconds

WordPress Trac noreply at wordpress.org
Thu Oct 25 21:27:36 UTC 2018

#45193: current_time() function will not return milliseconds / microseconds
 Reporter:  phillipburger  |      Owner:  (none)
     Type:  defect (bug)   |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  General        |    Version:
 Severity:  normal         |   Keywords:
  Focuses:                 |
 current_time() function in functions.php
 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!

Ticket URL: <https://core.trac.wordpress.org/ticket/45193>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform

More information about the wp-trac mailing list