Shell Suite
Shell Suite

Reputation: 690

Convert datetime in GMT+# format to GMT

i have an input format like this, and i need to convert it to GMT format:

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

the input data contain gmt array index which show the which gmt format, and the datetime index shows the date in "Y-m-d h:i:s" that needs to be convert from GMT+7 to GMT.

Upvotes: 4

Views: 234

Answers (2)

P Pang
P Pang

Reputation: 264

Oneshot (not recommended):

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

Upvotes: 0

Sami Almalki
Sami Almalki

Reputation: 628

Try this:

$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');

Upvotes: 1

Related Questions