Carbon::now() with time-offset result in different behaviors
Asked Answered
D

2

2

I'am using Carbon for manipulating dates in a laravel project.

Carbon::now('+5:30');

Above code is working fine in local environment but not in development environment.

This is what i get on dd(Carbon::now('+5:30'));

1 - IN LOCAL ENVIRONMENT php version - 5.6.3

enter image description here

2 - IN DEVELOPMENT ENVIRONMENT php version - 5.5.9-1ubuntu4.14

enter image description here

But both environment behaves same if i use timezone name instead of time-offset like,

 Carbon::now('Asia/Tokyo');

Is this something about the php-version or something else?

Dockage answered 6/4, 2016 at 9:45 Comment(0)
M
2

I have an answer for you after searching for related issues.

It seems that PHP version 5.5.9 had a bug:

https://mcmap.net/q/357502/-is-it-possible-to-create-a-datetimezone-from-an-offset

So you can't use that method with Carbon, but the following should work:

Carbon::now()->addHours(5)->addMinutes(30);

You can place your servers timezone in there for added accuracy:

Carbon::now(date_default_timezone_get())->addHours(5)->addMinutes(30);
Morion answered 3/6, 2016 at 11:15 Comment(0)
U
1

you can change in

'timezone' => 'UTC'

This time zone must match to your country zone.So replace this UTC with your current zone.

Undertrump answered 6/4, 2016 at 10:38 Comment(2)
Correct but what im asking is why its not working in some situations & i need to do as Carbon::now('+5:30') because i need to format some times from different countries.Dockage
Changing environment files can have unintended consequences in other places.Para

© 2022 - 2024 — McMap. All rights reserved.