Laravel-mongodb: PHP MongoDate toDateTime default timezone

Created on 6 Mar 2015  Â·  7Comments  Â·  Source: jenssegers/laravel-mongodb

The problem is the change in method DatabaseTokenRepository getPayload

getPayload protected function ($ email, $ token)
{
return ['email' => $ email, 'token' => $ token, 'created_at' => new MongoDate];
}

when using MongoDate think the timezone of MongoDate default is UTC

as explained here:

When I create new DateTime object, it has timezone from "date.timezone" setting:

print_r (new DateTime ());

DateTime Object
(
    [Date] => 03/02/2015 03: 19: 50.000000
    [Timezone_type] => 3
    [Timezone] => Europe / Moscow
)

But when i get DateTime object from MongoDate, it has UTC timezone:

print_r ((new MongoDate ()) -> ToDateTime ())

DateTime Object
(
    [Date] => 03/02/2015 00: 19: 50.000000
    [Timezone_type] => 1
    [Timezone] => +00: 00
)

How can I setup MongoDate to create DateTime objects with my default timezone?

http://stackoverflow.com/questions/28800996/php-mongodate-todatetime-default-timezone

Tomarce should consider the timezone of app.php as does new Carbon. in the modified part.

Most helpful comment

From the docs

MongoDB stores dates as milliseconds past the epoch. This means that dates do not contain timezone information

With a created_at field, you do not need to store the timezone in the db; coz all you need is to add the tz information to your DateTime object and can be set in the .ini file or using date_default_timezone_set

Either way, you now do

print_r((new \MongoDate())->toDateTime()->setTimezone(new \DateTimeZone(date_default_timezone_get())))

All 7 comments

Mongo date has no timezone; that means the created_at field should assume the server's timezone. When converting from MongoDate to DateTime, u'll have to set the target timezone.

I'm running everything locally so it should take my time zone

$ date
vie mar 6 11:03:36 ECT 2015

but is keeping the default UTC.

From the docs

MongoDB stores dates as milliseconds past the epoch. This means that dates do not contain timezone information

With a created_at field, you do not need to store the timezone in the db; coz all you need is to add the tz information to your DateTime object and can be set in the .ini file or using date_default_timezone_set

Either way, you now do

print_r((new \MongoDate())->toDateTime()->setTimezone(new \DateTimeZone(date_default_timezone_get())))

Hope it worked!

Sent from my iPhone

On Mar 7, 2015, at 3:42 AM, Alejandro Labrada Diaz [email protected] wrote:

Closed #441.

—
Reply to this email directly or view it on GitHub.

Easiest way:

//$mongoDate from db

$dateTime = new \DateTime();
$dateTime->setTimestamp($mongoDate->sec);

The default timezone should be set in ->toDateTime() call. And only if is necessary to set another timezone it should be set up manually.

$mongoDate->sec

Mongo date does't have sec attribute.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

geofflancaster picture geofflancaster  Â·  3Comments

YSimple picture YSimple  Â·  3Comments

imrannazirbhat picture imrannazirbhat  Â·  3Comments

pirmax picture pirmax  Â·  3Comments

phuocduy1988 picture phuocduy1988  Â·  3Comments