0

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;

     }
Zee
  • 351
  • 1
  • 15

1 Answers1

0

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.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • Bro, Where is this library. I have found one on git and tried to install but not working. I'm not using Mongo DB directly. Just posting data to that to use with my app which is being built by app developers. – Zee Oct 22 '15 at 06:33
  • Sorry, not sure what you mean. I tried though: $param = $dt->format('Y-m-d H:i:s'); if( $param ) { is_string( $param ) ? $date = new MongoDate( strtotime( $param ) ) : $date = new MongoDate( $param ); } else { $date = new MongoDate(); } echo $date; and get same Class 'MongoDate' not found in – Zee Oct 22 '15 at 06:43
  • check [example #1 on here](http://php.net/manual/en/class.mongodate.php#mongodate.intro) – Abdulla Nilam Oct 22 '15 at 06:45
  • That's what I tried originally, but getting same error of Class 'MongoDate' not found. :( – Zee Oct 22 '15 at 06:47
  • hey make sure extension is loaded. you can check that in `php.ini` – Abdulla Nilam Oct 22 '15 at 06:49
  • What am I looking in php.ini? – Zee Oct 22 '15 at 06:53
  • http://stackoverflow.com/questions/8927255/fatal-error-mongo-class-not-found chaeck this – Abdulla Nilam Oct 22 '15 at 06:57