【问题标题】:Convert date and time based on GMT根据 GMT 转换日期和时间
【发布时间】:2012-08-11 13:32:20
【问题描述】:

我在使用 GMT 时间转换日期和时间方面需要帮助。目前我正在使用Convertdatetime 使用时区,但我需要根据从下面的下拉列表中选择的GMT 转换日期和时间,仅使用GMT 和now() 函数用于将日期和时间存储到MySQL(服务器时间)?

例如 (2012/08/11 18:45:00) 阿富汗 至 2012 年 8 月 11 日星期六下午 5:43

安道尔 下午 3 点 16 分 2012 年 8 月 11 日星期六

<option value="Afghanistan">(GMT+4:30) Afghanistan</option>
<option value="Andorra">(GMT +1:00) Andorra</option>
<option value="United Arab Emirates">(GMT +04:00) United Arab Emirates</option>
<option value="Antigua and Barbuda">(GMT +04:00) Antigua and Barbuda</option>


function Convertdatetime($gmttime,$timezoneRequired)
{
    $system_timezone = date_default_timezone_get();

    $local_timezone = $timezoneRequired;
    date_default_timezone_set($local_timezone);
    $local = date("Y-m-d h:i:s A");

    date_default_timezone_set("GMT");
    $gmt = date("Y-m-d h:i:s A");

    date_default_timezone_set($system_timezone);
    $diff = (strtotime($gmt) - strtotime($local));

    $date = new DateTime($gmttime);
    $date->modify("+$diff seconds");
    $timestamp = $date->format("m-d-Y H:i:s");
    return $timestamp;
}
ConvertLocalTimezoneToGMT('2012-08-11 17:24:00.000','Asia/Calcutta');

输出:11-08-2012 11:54:00 || GMT = IST-5.5

【问题讨论】:

    标签: php mysql datetime


    【解决方案1】:

    要更改您的时区,您只需使用设置器setTimezone

    $systemTz = new DateTimezone($system_timezone);
    $gmt = new DateTimeZone('GMT');
    $serverTz = new dateTimezone($server_timezone);
    $dateServer = new DateTime($sqlResult['date_column'],$serverTz);
    $gmt = clone $dateServer;
    $gmt->setTimezone($gmt);
    $dateSyst = clone DateServer;
    $dateSyst->setTimezone($systemTz);
    

    然后你可以随意格式化它们。

    【讨论】:

      【解决方案2】:

      以下代码可以为您提供不同时区的秒数差异:

      $timezone = new DateTimeZone("Asia/Karachi");
      $offset = $timezone->getOffset(new DateTime("now"));
      

      Asia/Karachi替换为用户选择的任何内容,而不是简单地将这些秒数添加到时间

      DateTimeZone::getOffset — 返回与 GMT 的时区偏移量

      http://www.php.net/manual/en/class.datetimezone.php

      【讨论】:

      • @Muhammed Ahmad Zafer:感谢您的回复,但我已经从下拉列表中获得了 gmt 值,基于 gmt 值我需要转换为国家/地区的日期和时间
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 2016-11-12
      • 2011-10-07
      • 2012-07-24
      • 2018-07-07
      • 2021-05-02
      相关资源
      最近更新 更多