php - MongoDate aggregation with timezone correction -
i generating report of orders broken down date, month , year using aggregate group function. date stored in mongodate object in utc.
'$group' => array ( '_id' => array ( 'year' => array ( '$year' => '$created' ), 'month' => array ( '$month' => '$created' ), 'day' => array ( '$dayofmonth' => '$created' ), ) )
i have tried adding date object using project, seems cannot add mongodate object. (as seen here: https://stackoverflow.com/a/18854229/1069277)
'$project' => array ( 'created' => array ( '$add' => array ( '$created' => 8 * 60 * 60 * 1000 ) ) )
is adding field stores timezone corrected way achieve @ moment?
i go on using date operators aggregation , doing date math in order change timestamp value (which referenced answer doing, not bson date) lengthy , inefficient.
what want do conversion in code , pass in mongodate type.
here's round-trip code consider:
date_default_timezone_set('utc'); $dt = new datetime("2014-02-01", new datetimezone('pst')); echo var_dump( $dt ); echo $dt->gettimestamp(), "\n"; $mongo = new mongodate( $dt->gettimestamp() ); echo var_dump( $mongo ); echo $mongo->sec, "\n"; $new = new datetime( date('y-m-d', $mongo->sec ), new datetimezone('pst')); echo var_dump ($new);
so whether passing in values or working results, conversions in code. it's better way.
Comments
Post a Comment