将本地日期,时间和时区值转换为php中的utc时区

时间:2012-07-26 14:21:25

标签: php mysql datetime timezone

我正在开发一个iPhone应用程序,我从用户那里得到日期和时间(单独)并将其提交到我的php web服务。然后php web服务应该采用这个日期和时间并将其保存到utc时区。我已经读过,我还需要在php文件中将本地时区转换为utc。我在我的iPhone应用程序中将'Asia / Kolkata(IST)偏移1980'作为我的时区值,并将其发送到php文件。现在我如何将日期,时间和时区转换为utc日期。

我必须将日期转换为utc格式,以便我可以查询我的表格以获取不超过当地时间的数据。

提前致谢

问候
的Pankaj

2 个答案:

答案 0 :(得分:0)

结帐PHP DateTime();

基本上你会想要像下面那样处理它。

$date = new DateTime($dateFromiPhone);
$date->setTimeZone(new DateTimeZone("UTC"));

然后使用$date->format();格式化您所需的用法

答案 1 :(得分:0)

使用setTimeZone将时区转换为用户发送到UTC的任何内容:

    $utz = new DateTimeZone($input['timezone']);
    $udt = new DateTime($input['date'].' '.$input['time'], $utz);
    $udt->setTimezone(new DateTimeZone('UTC'));

    echo $input['date'].' '.$input['time'].' '.$input['timezone'].' is '.$udt->format('Y-m-d H:i:s')." UTC\n";