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')
Share:
127,908
Rob
Author by

Rob

Updated on July 09, 2022

Comments

  • Rob
    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 me Bad 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
      Yevgeniy Afanasyev almost 6 years
      is it the same as the question here?
  • Mladen Janjetovic
    Mladen Janjetovic over 8 years
    I was stuck because I tried to set timezone with createFromTimestamp, even if it accepts second param. Apparently, Timestamp is always in UTC...
  • Safoor Safdar
    Safoor Safdar over 8 years
    @mladen-janjetović can you show me your code, how you trying?
  • Safoor Safdar
    Safoor Safdar over 8 years
    if you want to change application wise format, you should setup config/app.timezone component as per your requirement.
  • Mladen Janjetovic
    Mladen Janjetovic over 8 years
    something 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
    Rodney Zanoria over 6 years
    is this to be added in the carbon.php file or the controller?
  • Etienne Marais
    Etienne Marais over 5 years
    Would Carbon take Daylight savings time into account?