Convert date + time in GMT + # format to GMT

I have an input format similar to this, and I need to convert it to GMT format:

$input = array(
           "gmt" => "+7",
           "datetime" => "2017-10-10 12:10:12"
         );

the input contains the gmt array index, which shows the gmt format, and the datetime index shows the date in "Ymd h: i: s", which should be converted from GMT + 7 to GMT.

+4
source share
2 answers

Try the following:

$input = array(
  "gmt" => "+7",
  "datetime" => "2017-10-10 12:10:12"
);

$ny = new DateTimeZone("GMT+7");
$gmt = new DateTimeZone("GMT");
$date = new DateTime( $input["datetime"], $ny );
$date->setTimezone( $gmt );

echo $date->format('Y-m-d H:i:s');
+1
source

Oneshot (not recommended):

echo date('Y-m-d h:i:s', strtotime($input['datetime'])+$input['gmt']*3600);
0
source

Source: https://habr.com/ru/post/1688505/


All Articles