Home > Blockchain >  PHP add minutes to MongoDB\BSON\UTCDateTime
PHP add minutes to MongoDB\BSON\UTCDateTime

Time:09-30

use MongoDB\BSON\UTCDateTime;

$date = new UTCDateTime($val * 1000); // 1659356187861

How can I do this if I want to subtract or add 5 minutes from this $date variable?

CodePudding user response:

According to the documentation: https://www.php.net/manual/en/class.mongodb-bson-utcdatetime.php

Since the UTCDateTime constructor accepts a DateTime object you can start with that, do the modifications, then create the UTCDateTime object using the DateTime:

use MongoDB\BSON\UTCDateTime;

// Start with @ to use a timestamp then add 5 minutes
$date = (new \DateTime('@' . $val))->modify(' 5 minute');

$mongoDate = new UTCDateTime($date);
  • Related