Php Mongodate Converting Date to 1970

529 views Asked by At

Hello guys I 'm storing date in mongodb. The problem that I'm facing is that the date time string that I get. I try to convert it into mongodate but it converts it to 0.00000000 2016. Here is the code

 $params['start'] = new MongoDate($params['start']);
        $params['end'] = new MongoDate($params['end']);

The string bring the date time in this form 2016-04-07 19:49:50 but after the conversion it becomes like this 0.00000000 2016. Please tell me what is it that I'm doing wrong

2

There are 2 answers

4
ceejayoz On BEST ANSWER

Per the docs, MongoDate expects a timestamp value like 1460058590, not a string like 2016-04-07 19:49:50.

$params['start'] = new MongoDate(strtotime($params['start']));
1
Mr. Llama On

The MongoDate constructor expects the time in Unix epoch seconds, not a time string.

public MongoDate::__construct ([ int $sec = time() [, int $usec = 0 ]] )

You'll need to convert your time string using strtotime or DateTime. The example code from the constructor documentation even includes an example:

$d = new MongoDate(strtotime("2009-05-01 00:00:01"));
echo "$d\n";