PHP 7.1.2 Released

The MongoDB\BSON\UTCDateTime class

(mongodb >=1.0.0)

Introduction

Represents a » BSON date.

Class synopsis

MongoDB\BSON\UTCDateTime implements MongoDB\BSON\Type , Serializable , JsonSerializable {
/* Methods */
final public __construct ([ integer|float|string|DateTimeInterface $milliseconds = NULL ] )
final public mixed jsonSerialize ( void )
final public string serialize ( void )
final public DateTime toDateTime ( void )
final public string __toString ( void )
final public void unserialize ( string $serialized )
}

Changelog

Version Description
1.2.0 Implements Serializable and JsonSerializable.

Table of Contents

add a note add a note

User Contributed Notes 1 note

up
2
mark at DONTSPAM dot moderndeveloperllc dot com
3 months ago
Be careful using UTCDateTime if you need microsecond precision as this class only stores time to the _millisecond_. You need to store the date as a string in MongoDB if you want the full precision. Casting DateTime to a float can also remove precision.

An easy way to see the differences is with:

Code:
var_dump(
  (new DateTime)->format('U.u'),
  (float) (new DateTime)->format('U.u'),
  (new MongoDB\BSON\UTCDateTime())->toDateTime()->format('U.u')
);

Output:
string(17) "1477534060.918415"
double(1477534060.9185)
string(17) "1477534060.918000"

NOTE: Lots of bugs concerning microsecond formatting for DateTimeInterface classes were fixed as of PHP 7.1.0 RC4. The above code is from PHP 7.1.0 RC5 and mongodb 1.2.0alpha3. The 1.2.0 version of the extension added the ability to instantiate this class without passing integer milliseconds.
To Top