Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $dt = new DateTime();
- echo $dt->format("Y-m-dTH:i:s.u") . "n";
- ?>
- [root@www1 ~]$ php date_test.php
- 2008-10-03T20:31:26.000000
- [root@www1 ~]$ php date_test.php
- 2008-10-03T20:31:27.000000
- [root@www1 ~]$ php date_test.php
- 2008-10-03T20:31:27.000000
- [root@www1 ~]$ php date_test.php
- 2008-10-03T20:31:28.000000
- function udate($format, $utimestamp = null)
- {
- if (is_null($utimestamp))
- $utimestamp = microtime(true);
- $timestamp = floor($utimestamp);
- $milliseconds = round(($utimestamp - $timestamp) * 1000000);
- return date(preg_replace('`(?<!\\)u`', $milliseconds, $format), $timestamp);
- }
- echo udate('H:i:s.u'); // 19:40:56.78128
- function getTimestamp()
- {
- return date("Y-m-dTH:i:s") . substr((string)microtime(), 1, 8);
- }
- class ExtendedDateTime extends DateTime {
- /**
- * Returns new DateTime object. Adds microtime for "now" dates
- * @param string $sTime
- * @param DateTimeZone $oTimeZone
- */
- public function __construct($sTime = 'now', DateTimeZone $oTimeZone = NULL) {
- // check that constructor is called as current date/time
- if (strtotime($sTime) == time()) {
- $aMicrotime = explode(' ', microtime());
- $sTime = date('Y-m-d H:i:s.' . $aMicrotime[0] * 1000000, $aMicrotime[1]);
- }
- // DateTime throws an Exception with a null TimeZone
- if ($oTimeZone instanceof DateTimeZone) {
- parent::__construct($sTime, $oTimeZone);
- } else {
- parent::__construct($sTime);
- }
- }
- }
- $oDate = new ExtendedDateTime();
- echo $oDate->format('Y-m-d G:i:s.u');
- 2010-12-01 18:12:10.146625
- date_create_from_format('U.u', microtime(true))->format('Y-m-d H:i:s.u');
Add Comment
Please, Sign In to add comment