/** * @param string $dt * @param \DateTimeZone|null $tz */ public function __construct($dt = 'now', $tz = null) { if (is_null($dt) || $dt === 'now') { $date = \DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', '')); } else { $date = new \DateTime($dt, $tz); } parent::__construct($date->format(self::FULL_ISO8601)); }
/** * @see http://stackoverflow.com/questions/17909871/getting-date-format-m-d-y-his-u-from-milliseconds * * @todo Support microtimes on relative formats: http://us3.php.net/manual/en/datetime.formats.relative.php * * @param string $time * @param \DateTimeZone $timezone */ public function __construct($time = self::NOW, \DateTimeZone $timezone = null) { $microtime = '0'; if ($time === self::NOW) { $microtime = microtime(true); $microtime = \sprintf('%06d', ($microtime - \floor($microtime)) * 1000000); } parent::__construct(\date('Y-m-d H:i:s.' . $microtime, strtotime($time)), $timezone); }
public function __construct($useMicroseconds, \DateTimeZone $timezone = null) { $date = 'now'; if ($useMicroseconds) { // Circumvent DateTimeImmutable::createFromFormat() which always returns \DateTimeImmutable instead of `static` // @link https://bugs.php.net/bug.php?id=60302 $timestamp = microtime(true); $microseconds = sprintf("%06d", ($timestamp - floor($timestamp)) * 1000000); $date = date('Y-m-d H:i:s.' . $microseconds, (int) $timestamp); } parent::__construct($date, $timezone); $this->useMicroseconds = $useMicroseconds; }
public function __construct($useMicroseconds, \DateTimeZone $timezone = null) { $this->useMicroseconds = $useMicroseconds; $date = 'now'; if ($useMicroseconds) { $timestamp = microtime(true); // apply offset of the timezone as microtime() is always UTC if ($timezone && $timezone->getName() !== 'UTC') { $timestamp += (new \DateTime('now', $timezone))->getOffset(); } // Circumvent DateTimeImmutable::createFromFormat() which always returns \DateTimeImmutable instead of `static` // @link https://bugs.php.net/bug.php?id=60302 // // So we create a DateTime but then format it so we // can re-create one using the right class $dt = self::createFromFormat('U.u', sprintf('%.6F', $timestamp)); $date = $dt->format('Y-m-d H:i:s.u'); } parent::__construct($date, $timezone); }
/** * @param string $time * @param \DateTimeZone|null $timezone */ public function __construct($time = "now", $timezone = NULL) { parent::__construct($time, $timezone); }