MongoDate::__construct

(PECL mongo >= 0.8.1)

MongoDate::__constructCreates a new date

Beschreibung

public MongoDate::__construct ([ int $sec = time() [, int $usec = 0 ]] )

Creates a new date. If no parameters are given, the current time is used.

Parameter-Liste

sec

Number of seconds since the epoch (i.e. 1 Jan 1970 00:00:00.000 UTC).

usec

Microseconds. Please be aware though that MongoDB's resolution is milliseconds and not microseconds, which means this value will be truncated to millisecond resolution.

R√ľckgabewerte

Returns this new date.

Beispiele

Beispiel #1 MongoDate::__construct() example

This example demonstrates creating a new date with the current time and a new date with a given time.

<?php

$d 
= new MongoDate();
echo 
"$d\n";
$d = new MongoDate(1234567890);
echo 
"$d\n";
$d = new MongoDate(strtotime("2009-05-01 00:00:01"));
echo 
"$d\n";

?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

0.23660600 1235685067
0.00000000 1234567890
0.00000000 1241150401

Siehe auch

add a note add a note

User Contributed Notes 1 note

up
1
gb at tekkie dot ro
6 years ago
Please note that the second parameter should be a microseconds value, not a miliseconds one, as stated in the documentation.

I am using the code below to show you what I mean:
<?php
    $example
= new \DateTime('2013-09-22T10:41:44.451999');
   
$seconds = $example->getTimestamp();

   
// this is what documentation misleads you to think is needed
   
$miliseconds = floor($example->format('u') / 1000);

   
// this is what you should really use
   
$microseconds = $example->format('u');
   
$fullToString = $example->format('Y-m-d\TH:i:s.uP');

   
$collection->insert(array(
       
'timeMicroseconds' => new \MongoDate($seconds, $microseconds),
       
'toString' => $fullToString,
    ));
   
$collection->insert(array(
       
'timeMiliseconds' => new \MongoDate($seconds, $miliseconds),
       
'toString' => $fullToString,
    ));
?>

Let's take a look in the database to see what is really stored:
{
    "_id" : ObjectId("523f07cfc2b581eb7f069b1d"),
    "timeMicroseconds" : ISODate("2013-09-22T07:41:44.451Z"),
    "toString" : "2013-09-22T10:41:44.451999+03:00"
}

{
    "_id" : ObjectId("523f07cfc2b581eb7f069b1e"),
    "timeMiliseconds" : ISODate("2013-09-22T07:41:44.000Z"),
    "toString" : "2013-09-22T10:41:44.451999+03:00"
}
To Top