Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

birdietorerik's avatar

Convert from UTC to local dateTime

Hi! Strugle to get this to work:

Created a command in laravel, to test this

 public function testUTC(){
     
        $timestamp = "2024-08-12 10:55:14";
 
        $date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp);
        $date->setTimezone('Europe/Stockholm');

        var_dump("Carbon : " .$date);
    
    }

This wil print out -> 2024-08-12 10:55:14 But this is UTC datetime, want localdatetime Timezone set to -> Europe/Stockholm

In this example it must return -> 2024-08-12 12:55:14

How do i do this ?

0 likes
3 replies
martinbean's avatar

@birdietorerik Use the timezone method, not setTimezone. This will set the timezone, and also change the date/time:

$timestamp = '2024-08-12 10:55:14';

$utc = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp);

$stockholmTime = $date->timezone('Europe/Stockholm');

// $stockholmTime will now be set to 12:55:14

It would also be nice to get responses to comments on existing questions before creating new ones.

Thriddle's avatar

Could you try this?

var_dump("Carbon : " .$date->toDateTimeLocalString());

Please or to participate in this conversation.