Search code examples
phpphp-carbon

Carbon Timezone and format not working together


Here is my code

$time = '2019-12-16T10:14:35.000000Z';

$var = new Carbon($time);

$var = $var->setTimezone('Asia/Dhaka')->format('Y-m-d H:i:s');

The return I am expecting is '2019-12-16 4:14:35'

The return I am getting '2019-12-16 10:14:35'

If I remove the format function, I am getting the right timezone '2019-12-16T4:14:35.000000Z'

Clearly the setTimeZone is not working with Format. What am I missing here?

I know there might be possible duplicate issue, but none of the solutions seems to work for me.


Solution

  • If your source timestamp always in UTC timezone. so change your code to:

    $var = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'UTC');
    $var = $var->setTimezone('Asia/Dhaka');