Home > Blockchain >  Converting Time with timezone PHP
Converting Time with timezone PHP

Time:12-31

I'd like to convert value/date/time that I got from callback raw value that I got is like this

$value='2021-01-20T19:03:52.355 0300';

I need to convert it into like this

$value='20-01-2021 23.03.52,355000  07:00';

what I've done some substr and concat but unfortunately it ends up with string and my db datatype format is timestamp and i can't insert the value to db

read some about DateTime::createFromFormat and I can convert the time format but still no clue for converting to another timezone

CodePudding user response:

You could change the timezone using setTimezone() :

$value    = '2021-01-20T19:03:52.355 0300';
$expected = '20-01-2021 23.03.52,355000  07:00';

$datetime = new \DateTime($value);        
$datetime->setTimezone(new \DateTimeZone(' 0700'));

var_dump($datetime->format('d-m-Y H.i.s,u P') == $expected); // bool(true)

CodePudding user response:

you can do like this

$datetime = new \DateTime('2021-01-20T19:03:52.355 0300');        
date_format($datetime, 'd-m-Y H.i.s,u P');

CodePudding user response:

You can try this code, this will work for you and you can set the timezone as per your need.

// Input  : '2021-01-20T19:03:52.355 0300';
// Output : '20-01-2021 23.03.52,355000  07:00';

date_default_timezone_set('Europe/London');
$datetime = new DateTime('2021-01-20T19:03:52.355 0300');

// timezone to convert.
$la_time = new DateTimeZone('Asia/Krasnoyarsk');
$datetime->setTimezone($la_time);
echo $datetime->format('d-m-Y H.i.s,u P');

Output:

20-01-2021 23.03.52,355000  07:00
  • Related