Re: Difference In Dates in Days - Cannot Subtract String From String Error

From: Wan Bachtiar <wan.bachtiar@xxxxxxxxxxx>
To: mongodb-user <mongodb-user@xxxxxxxxxxxxxxxx>
Date: Wed, 11 May 2016 02:15:26 -0700 (PDT)
Why ads?


How can I cast or subtract these as dates and get the days in between the 
dates to compare to another var

Hi, 

You could utilise MongoDB\BSON\UTCDateTime 
<http://php.net/manual/en/class.mongodb-bson-utcdatetime.php> class to get 
a representation of BSON date 
<https://docs.mongodb.com/manual/reference/bson-types/#date> in PHP.
For example: 

/* reminder time in milliseconds */$reminder_interval = 2*60*1000; 
/* Get current time in milliseconds */$current_time = new MongoDB\BSON\UTCDateTime(microtime(true)*1000);
/* An example of last interacted time converted into BSON date */$last_interacted_date = "2016-05-11T08:11:42Z";$last_time = new MongoDB\BSON\UTCDateTime(strtotime($last_interacted_date)*1000);
$query = array(
           array('$redact' =>
             array('$cond' => 
               array("if" => 
                 array('$gte' => 
                   array(
                     array('$subtract' => array($current_time, $last_time)),
                     $reminder_interval
                   )
                 ),
                 "then" => '$$KEEP',
                 "else" => '$$PRUNE'
               )
             )
           )
         );

Note that the result of the subtraction above is in milliseconds, hence the 
$reminder_interval is also in milliseconds.

The above snippet was tested using MongoDB PHP driver 
<https://docs.mongodb.com/ecosystem/drivers/php/> v1.1, PHP 7.0 and MongoDB 
v3.2. 

Regards, 

Wan


-- 
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
--- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@xxxxxxxxxxxxxxxx.
To post to this group, send email to mongodb-user@xxxxxxxxxxxxxxxx.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/8ebbe067-6fe3-401a-a381-c3a6a3f0f574%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Why ads?