2017-03-17 121 views
1

需要在IST(印度標準時間)當前時間,存儲在PHP變量UTC時間轉換前一小時,並使用PHP其存儲到另一個變量。如何將目前的IST時間轉換爲UTC時間?還需要找到時間UTC時間

<?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)); 
?> 
+1

[PHP轉換日期時間爲UTC(可能的重複http://stackoverflow.com/questions/2095703/php-convert-datetime-to-utc) – NID

回答

0

請仔細閱讀這一點,應該解決您的問題

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

例如:

$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 
1

使用gmdate

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

˚F ollowing將減少1小時

echo gmdate('Y-m-d H:i', strtotime($Present.'-1 hour')); 
+0

我還需要在這個時間前一小時轉換一個變量。代碼更新如上。 –

+0

你能幫我嗎? –

+0

是的!更新:) –

0
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'); 
+0

只有代碼的答案是不鼓勵的。請解釋你的解決方案。 – Blackbam

0

嘗試建在getTimezone and setTimezone PHP中,

$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 />"); 
相關問題