How to use MongoDate in Codeigniter

321 views Asked by At

I have to convert date time before sending to mongodb. By googling found that I should use MongoDate class.

/* Fields */
Class MongoDate {

/* Fields */
public int $sec ;
public int $usec ;
/* Methods */
public __construct ([ int $sec = time() [, int $usec = 0 ]] )
public DateTime toDateTime ( void )
public string __toString ( void )

}

How to use inside my codeigniter project? I have tried creating a helper file and put the code exactly in there but got error unexpected 'int' when I remove 'int' from both $sec and $usec then I get error of syntax error, unexpected '__construct'

Thanks for help in advance.


Edited

This is my function

public function do_placeorder(){


         $url   =   'https://myapp.herokuapp.com/api/orders';

         $ColDate   =   $this->session->userdata('ColRealDate');
         $dt = new DateTime("@$ColDate");
         include_once(APPPATH.'helpers/mongodate_helper.php');
         $ColDT = new MongoDate(strtotime($dt->format('Y-m-d H:i:s')));
         echo $ColDT;

     }
1

There are 1 answers

7
Abdulla Nilam On

In mangoDB library, create function like this

public function generate_date( $param = null ) {
    if( $param ) {
        is_string( $param ) ? $date = new MongoDate( strtotime( $param ) ) : $date = new MongoDate( $param );
    } else {
        $date = new MongoDate();
    }
    return $date;
}

and pass parameter to this.