Format Timezone for Carbon Date
127,908
Solution 1
You can change the timezone with this:
$timestamp = '2014-02-06 16:34:00';
$date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'Europe/Stockholm');
$date->setTimezone('UTC');
this format working fine to my Local(Ubuntu) and prod(Redhat) project.
Solution 2
I think you should refer to the official php timezone list. In your case you can use
$date->setTimezone('Asia/Phnom_Penh');
for UTC+7:00.
Solution 3
For people wondering, you can chain the timezone like so:
Carbon::now()->timezone('Europe/Stockholm')
Author by
Rob
Updated on July 09, 2022Comments
-
Rob almost 2 years
I'm trying to set the timezone for a date in a
Carbon
object. It works fine locally but on my production box it keeps giving meBad timezone
error.I've tried:
$date->setTimezone('7'); $date->setTimezone('+7'); $date->setTimezone('7:00'); $date->setTimezone('+7:00'); $date->setTimezone('UTC 7'); $date->setTimezone('UTC +7'); $date->setTimezone('UTC 7:00'); $date->setTimezone('UTC +7:00');
No idea why it's complaining on my production box. Can't find documentation either on what is the "proper" format to enter here. Can someone please help.
FYI: local is windows, and prod is Ubuntu box.
-
Yevgeniy Afanasyev almost 6 yearsis it the same as the question here?
-
-
Mladen Janjetovic over 8 yearsI was stuck because I tried to set timezone with createFromTimestamp, even if it accepts second param. Apparently, Timestamp is always in UTC...
-
Safoor Safdar over 8 years@mladen-janjetović can you show me your code, how you trying?
-
Safoor Safdar over 8 yearsif you want to change application wise format, you should setup config/app.timezone component as per your requirement.
-
Mladen Janjetovic over 8 yearssomething like this if i remember well
$date = Carbon::createFromTimestamp($timestamp, 'EST');
It is not returning right time when call ->timezone('UTC') on this. app.timezone is set to UTC. With this createFromFormat it works well. -
Rodney Zanoria over 6 yearsis this to be added in the carbon.php file or the controller?
-
Etienne Marais over 5 yearsWould Carbon take Daylight savings time into account?