php - UTCDateTime::toDateTime() method returnig 1970 datetime

Solution:
Choose any one step for your code
STEP 1 :
new MongoDB\BSON\UTCDateTime( (int) $timestamp * 1000 );
STEP 2 :
You can use below function instead of direct assigning. Example : mongoDate('1542957787');
function mongoDate( $timestamp = '' ) {
if ( empty($timestamp) && $timestamp != 0 ) {
return false;
}
if ( strlen( (string) $timestamp ) == 13 ) { // timestamp length in MilliSeconds
return new MongoDB\BSON\UTCDateTime( (int) $timestamp );
}
return new MongoDB\BSON\UTCDateTime( (int) $timestamp * 1000 );
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: malformed utf-8 characters, possibly incorrectly encoded
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.