伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: MongoDate

来源:网络转载 浏览:56次 时间:2022-11-19
MongoDate::__construct » « MongoCode::__toString PHP 手册 函数参考 数据库扩展 针对各数据库系统对应的扩展 Mongo Types

The MongoDate class

(PECL mongo >=0.8.1)

Warning

This extension that defines this class is deprecated. Instead, the MongoDB extension should be used. Alternatives to this class include:

MongoDB\BSON\UTCDateTime

简介

Represent date objects for the database. This class should be used to save dates to the database and to query for dates. For example:

Example #1 Storing dates with MongoDate

<?php

// save a date to the database
$collection->save(array("ts" => new MongoDate()));

$start = new MongoDate(strtotime("2010-01-15 00:00:00"));
$end = new MongoDate(strtotime("2010-01-30 00:00:00"));

// find dates between 1/15/2010 and 1/30/2010
$collection->find(array("ts" => array('$gt' => $start, '$lte' => $end)));

?>

MongoDB stores dates as milliseconds past the epoch. This means that dates do not contain timezone information. Timezones must be stored in a separate field if needed. Second, this means that any precision beyond milliseconds will be lost when the document is sent to/from the database.

类摘要 MongoDate { /* Fields */ public int $sec ; public int $usec ; /* 方法 */ public __construct ([ int $sec = time() [, int $usec = 0 ]] ) public toDateTime ( void ) : DateTime public __toString ( void ) : string } Table of ContentsMongoDate::__construct — 创建一个新的日期。MongoDate::toDateTime — Returns a DateTime object representing this dateMongoDate::__toString — 返回该日期的字符串形式的表达
add a note

User Contributed Notes 4 notes

up down 17 jhonnydcano at yahoo dot com9 years ago For showing a human readable MongoDate, you can use something like this:

<?php
date('Y-M-d h:i:s', $yourDate->sec);
?>
up down 2 Philipp2 years ago This extension is deprecated, but some people still have to use it.

Please, note that properties (sec, usec) are read-only.

You have to do something like this:

<?php

$d = new \MongoDate();

echo "\n" . $d->toDateTime()->format(\DateTime::ISO8601);

// move to future for 125 seconds
$d = new \MongoDate($d->sec + 125, $d->usec);

echo "\n" . $d->toDateTime()->format(\DateTime::ISO8601);

?>
up down -1 mike at brenden dot com1 year ago MichaelBrenden --

How do the two Mongo drivers determine if they can translate something into the ISODate format?

Does mongo.so require a class type of MongoDate, or does it determine by data returned by __toString() (i.e., "0.12345678 1234567890")?

Does mongodb.so require a class type of \MongoDB\BSON\UTCDateTime, or does it determine by data returned by __toString() (i.e., "1234567890123")?

jmikola

>> the ISODate format?

For the record, ISODate is just a helper function in the mongo shell. The actual BSON type is 0x09 in the BSON spec and is best referred to as "UTC date time".
[url]https://docs.mongodb.com/manual/core/shell-types/#return-date[/url]
[url]http://bsonspec.org/spec.html[/url]

>> Does mongodb.so require a class type of \MongoDB\BSON\UTCDateTime, or does it determine by data returned by __toString() (i.e., "1234567890123")?

The driver requires a MongoDB\BSON\UTCDateTime object in order to encode a BSON date type. The relevant instanceof check during BSON encoding is here if you care to see it. The __toString() function exists purely as a convenience for userland applications. The driver does not use it outside of tests.
[url]https://github.com/mongodb/mongo-php-driver/blob/1.3.4/src/bson-encode.c#L215[/url]

The legacy mongo.so driver is similar in that it requires a MongoDate object in order to encode a BSON date type (relevant code is here).
[url]https://github.com/mongodb/mongo-php-driver-legacy/blob/1.6.16/bson.c#L273[/url]
up down -44 richard at securebucket dot com7 years ago I wanted a way to check the age of a record.  This code will calculate the time between the creation date of the record, and return the seconds.  I use it for caching pages similar to the max-age header in a browser.  If the record is older, than my limit it deletes itself.

<?php
function microtime_diff( $start, $end=NULL ) {
        if( !$end ) {
            $end= microtime();
        }
        list($start_usec, $start_sec) = explode(" ", $start);
        list($end_usec, $end_sec) = explode(" ", $end);
        $diff_sec= intval($end_sec) - intval($start_sec);
        $diff_usec= floatval($end_usec) - floatval($start_usec);
        return floatval( $diff_sec ) + $diff_usec;
}

microtime_diff($cache['stamp']->sec);
?>
add a note

官方地址:https://www.php.net/manual/en/class.mongodate.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net