1

Need to convert the current time in IST (India Standard Time), stored in a PHP variable to UTC time, and store it to another variable using PHP.

date_default_timezone_set('Asia/Kolkata');
$Present = date('Y-m-d H:i');
echo $Present; //to be converted to utc


$GMT = gmdate('Y-m-d H:i', strtotime($Present));
Raunak Gupta
  • 10,412
  • 3
  • 58
  • 97
Aastha Kathuria
  • 53
  • 1
  • 10
  • 1
    Possible duplicate of [php convert datetime to UTC](http://stackoverflow.com/questions/2095703/php-convert-datetime-to-utc) – NID Mar 17 '17 at 07:29

5 Answers5

1

Use gmdate

echo gmdate('Y-m-d H:i', strtotime($Present));

following will decrease 1 hour

echo gmdate('Y-m-d H:i', strtotime($Present.'-1 hour'));
Manikandan
  • 502
  • 1
  • 7
  • 17
1
date_default_timezone_set('Asia/Kolkata');
$Present=date('Y-m-d H:i');
echo $Present; //to be converted to utc

$date = new DateTime($Present);
$date->setTimezone(new DateTimeZone('Africa/Abidjan'));
echo $date->format('Y-m-d H:i');
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
0

Try the php built in getTimezone and setTimezone,

$the_date = strtotime("2010-01-19 00:00:00");
echo(date_default_timezone_get() . "<br />");
echo(date("Y-d-mTG:i:sz",$the_date) . "<br />");
echo(date_default_timezone_set("UTC") . "<br />");
echo(date("Y-d-mTG:i:sz", $the_date) . "<br />");
Hiren Makwana
  • 1,976
  • 2
  • 13
  • 28
0

Here is the simple and tested solution

$presentInst = new \DateTime(now('Asia/Kolkata'), new \DateTimeZone('Asia/Kolkata'));
$present     = $presentInst->format("Y-m-d H:i:s e");

echo $present . "<br/>"; // your local time i.e. in IST

$presentInst->setTimezone(new \DateTimeZone("UTC"));
$utcTime = $presentInst->format("Y-m-d H:i:s e");

echo $utcTime; //UTC time

Hope this helps

Raunak Gupta
  • 10,412
  • 3
  • 58
  • 97
-1

Please read this, it should solve your problem

http://php.net/manual/en/datetime.gettimezone.php

example :

$given = new DateTime("2014-12-12 14:18:00");
echo $given->format("Y-m-d H:i:s e") . "\n"; // your time

$given->setTimezone(new DateTimeZone("UTC"));
echo $given->format("Y-m-d H:i:s e") . "\n"; // UTC
gmed
  • 140
  • 1
  • 10