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

Crazylife's avatar

How to create from format with timezone using carbon?

I have a string

$string = "180711000000GMT+0800"

// try to convert it 
Carbon::createFromFormat('YmdHisTO', $string)->format...

It shows me this error.

Unexpected data found.
The timezone could not be found in the database
Data missing

How can i convert the datetime if there's GMT+0800 behind of the string?

0 likes
3 replies
pardeepkumar's avatar

i suggest you to try like

$string = '2018-09-09 16:34:00';
$date = Carbon::createFromFormat('Y-m-d H:i:s', $string, 'US/..');
$date->setTimezone('UTC');
1 like
Crazylife's avatar

@pardeepkumar I am getting the file format from integration, they will pass me a format like GMT+8 or GMT+0800, how can i store the datetime in my database?

tykus's avatar

I don't know if there is support for createFromFormat with a timezone offset, but you could split the string:

preg_match('/^(\d{12})(\w+\+\d{4})$/', $string, $matches);
[,$datetime, $tzOffset] = $matches;

Carbon::createFromFormat('ymdHis', $datetime, $tzOffset)->format...

Please or to participate in this conversation.