getTimestamp() public method

(PECL mongo >= 1.0.1) Gets the number of seconds since the epoch that this id was created
public getTimestamp ( ) : integer
return integer
 public function testCreateWithString()
 {
     $original = '54203e08d51d4a1f868b456e';
     $id = new \MongoId($original);
     $this->assertSame($original, (string) $id);
     $this->assertSame(9127278, $id->getInc());
     $this->assertSame(1411399176, $id->getTimestamp());
     $this->assertSame(34335, $id->getPID());
 }
示例#2
0
 /**
  * @param $mongoId
  */
 public function assertMongoId($mongoId)
 {
     if (is_string($mongoId)) {
         $mongoId = new \MongoId($mongoId);
     }
     $this->assertTrue($mongoId instanceof \MongoId);
     $this->assertNotNull($mongoId->getPID());
     $this->assertNotNull($mongoId->getTimestamp());
     $this->assertNotNull($mongoId->getInc());
     $this->assertNotNull($mongoId->getHostname());
     $this->assertEquals(24, strlen($mongoId->{'$id'}));
 }
示例#3
0
 /**
  * Check if given string is a potentially valid MongoId string<br>
  *
  * NOTE: This check is not bullet proof but is useful in most situations.
  *
  * @param $id
  *
  * @return bool
  */
 public static function isMongoId($id)
 {
     if ($id instanceof \MongoId) {
         return true;
     }
     if (!self::isString($id)) {
         return false;
     }
     $match = self::str($id)->match('[0-9a-f]{24}', false);
     if (!$match) {
         return false;
     }
     $mongoId = new \MongoId($match[0]);
     return $mongoId->getTimestamp() > 0;
 }
示例#4
0
 public function testTimestamp()
 {
     $time = time();
     $id = new MongoId();
     $this->assertTrue(abs($time - $id->getTimestamp()) < 1000, $time - $id->getTimestamp());
 }
示例#5
0
文件: mongo-type.php 项目: isS/NoSQL
<?php

MongoId::getHostname();
MongoId::__toString();
MongoId::getTimestamp();
MongoId::isValid();
MongoId::__set_state();
MongoId::getInc();
MongoId::getPID();
$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"));
$collection->find(array("ts" => array('$gt' => $start, '$lte' => $end)));
$search = "V";
$where = array('name' => array('
	$regex' => new MongoRegex("/^{$search}/")));
$cursor = $collection->find($where);
$where = array('name' => array('$regex' => new MongoRegex("/^{$search}/i")));
$profile = array("username" => 'foobity', "pic" => new MongoBinData(file_get_contents("gravatar.jpg"), MongoBinData::GENERIC));
$connection->save($profile);
$people = $db->people;
$addresses = $db->addresses;
$myAddress = array("line 1" => '12 main sreet', 'line 2' => null, 'city' => 'beijing', 'state' => "vermont", 'country' => 'china');
$addresses->insert($myAddress);
$me = array('name' => 'fred', 'address' => $myAddress['_id']);
$people->insert($me);
session_start();
/*Code written by Ashish Trivedi*/
//including common mongo connection file
include '../mongo_connection.php';
//generating a new post id
$post_id = new MongoId();
//getting user id from the ajax POST parameters
$user_id = new MongoId($_POST['user_id']);
//Session User ID
$user_session_id = new MongoId($_SESSION['user_id']);
//getting post text from the ajax POST parameters
$post_text = $_POST['post_text'];
//generating post timestamp from the post id
//the statement should differ based on the date format needed and time zone
$timestamp = date('D, d-M-Y', $post_id->getTimestamp() + 19800);
//Check if the user ID coming from AJAX parameter is same as Session User ID
if ($user_id == $user_session_id) {
    //selecting posts_collection
    $collection = $database->selectCollection('posts_collection');
    //creates document for inserting new post
    $new_post_mongo = array('_id' => $post_id, 'post_author_id' => $_SESSION['user_id'], 'post_text' => $post_text, 'total_likes' => 0, 'likes_user_ids' => array(), 'comments' => array(), 'total_comments' => 0, 'timestamp' => $timestamp);
    $collection->insert($new_post_mongo);
}
?>
         

<!-- Returns html data for the newly inserted post. 
This data will be sent as output to the AJAX POST call. 
Then from the javascript, this data containing the new post will be prepended to the page.
-->