Is there something wrong with the MongoDate feature?
When I convert a DateTime Object to MongoDate, and then I try to convert it back to DateTime, I get a totally different value. Keep in mind we're talking about a date within the EPOCH limits.
Here's a way to reproduce the issue.
$dateTime = new DateTime( '2015-07-20 10:15:45', new DateTimeZone( 'Europe/London' ) );
$mongoDate = new MongoDate( $dateTime->getTimeStamp() );
echo $mongoDate->sec ."\n"; // 1437383745
echo date( 'Y-m-d H:i:s', $mongoDate->sec ) ."\n"; // 2015-07-20 11:15:45
$dateTime2 = $mongoDate->toDateTime();
echo $dateTime2->format('Y-m-d H:i:s') ."\n"; // 1969-12-15 10:41:40
Is this behaviour normal?
You have timezone issue. When you create DateTime object, you create it in Europe/London
timezone. But when you "convert" seconds back to datetime format, you use date()
function, which uses default timezone, which apparently is different from Europe/London
.
Create DateTime object with $mongoDate->sec
and then convert timezone.
$dt = new DateTime('@' . $mongoDate->sec);
$dt->setTimezone(new DateTimeZone('Europe/London'));
echo $dt->format('c');