【问题标题】:Convert local date time to UTC date time将本地日期时间转换为 UTC 日期时间
【发布时间】:2014-01-22 08:24:19
【问题描述】:

当用户根据他/她的时区使用 'input type=date name="date"' && 'input type=time name="time"' 标签输入日期和时间时,

例如:- 如果来自印度(亚洲/加尔各答)时区的用户输入日期:2014 年 1 月 22 日和时间:下午 5:30,我需要将其转换为 UTC 时间戳以将其存储到数据库,实现这一目标

我使用了以下代码:-

$year=substr($date,0,4);
$month=substr($date,5,2);
$day=substr($date,8);
$hour=substr($time,0,2);
$minute=substr($time,3);
$clientzone=mysql_result(mysql_query("select timezone from c_users_extra where  c_id='{$_SESSION['clientid']}'"),0,0); //Fetches the timezone of user
date_default_timezone_set($clientzone);//Setting default timezone to client timezone
$datetime = new DateTime("$year-$month-$day $hour:$minute:00");
$la_time = new DateTimeZone('UTC');
$datetime->setTimezone($la_time);
$values=$datetime->format('Y-m-d H:i:s');
$year=substr($values,0,4);
$month=substr($values,5,2);
$hour=substr($values,11,2);
$minutes=substr($values,14,2);
$seconds=substr($values,17,2);
$timestamp=mktime($hour,$minutes,$seconds,$month,$day,$year);//creating new timestamp from coverted 
print_r(getdate($timestamp));//Result :  Array ( [seconds] => 0 [minutes] => 30 [hours] => 6 [mday] => 22 [wday] => 3 [mon] => 1 [year] => 2014 [yday] => 21 [weekday] => Wednesday [month] => January [0] => 1390372200 )
//Expected Result : Array ( [seconds] => 0 [minutes] => 0 [hours] => 12 [mday] => 22 [wday] => 3 [mon] => 1 [year] => 2014 [yday] => 21 [weekday] => Wednesday [month] => January [0] => 1390372200 ) 

为什么我得到这个错误的时间戳?

【问题讨论】:

  • 抱歉,这太过分了,为什么要使用所有这些substrs?什么时候可以直接获取这些值
  • [offtopic]当我看到通过 substr 在 PHP 中获取日期的年/月/日时,我感觉自己很年轻...[/offtopic] 在您的问题中添加所需的结果跨度>
  • 从日期和时间输入标签中检索准确的值..
  • 只是为了打个时间戳?当DateTime对象offers that functionality?
  • 需要将用户输入的日期和时间从用户时区转换为UTC时区

标签: php datetime


【解决方案1】:

这是一个面向对象的例子:

<?php
  $date = '2014-01-22 18:15:00';  // assumed date is formatted correctly 
  $clientzone = 'America/New_York';  // assumed timezone is a valid one from your SQL
  $dateObj = new DateTime($date, new DateTimeZone($clientzone));
  echo "Original: " . $dateObj->format('Y-m-d H:i:sP') . "\n";

  $dateObj->setTimezone(new DateTimeZone('UTC')); // convert to UTC
  echo "Converted: " . $dateObj->format('Y-m-d H:i:sP') . "\n";
  echo "Epoch: ".$dateObj->format('U');
?>

您可以像日期函数一样对其进行格式化。 假定 $date 和 $clientzone 有效。

【讨论】:

    【解决方案2】:

    你不能做一些更简单的事情吗:

    $user_time = strtotime($date);
    $dateTimeZone = new DateTimeZone($timezone);
    // get the offset from server time (UTC)
    $tzOffset = $dateTimeZone->getOffset(new DateTime());
    $result_time = $user_time - $tzOffset;
    

    【讨论】:

      【解决方案3】:

      尝试以下函数将本地日期时间转换为UTC日期时间

      function LocaltoUTC($date_to_convert)
      {
          $local_timestamp = strtotime($date_t);
      
          $UTC_timestamp += ((-(5.5)) * 3600);  // 5.5 is UTC timezone or local timezone
      
          $gmt_datetime = gmdate('y-m-d H:i:s', $UTC_timestamp); 
          return $gmt_datetime;
      }
      

      【讨论】:

      • 使用 unix 时间戳和硬编码时区偏移的丑陋解决方案,不考虑夏令时,而不是使用 DateTime 和 DateTimeZone 对象
      • @Mark Ba​​ker - 硬编码时区仅用于示例。开发人员必须提出自己的逻辑来获取时区。我并不是说我的解决方案已经可以使用了,但正如我所说的尝试并使其成为面糊
      【解决方案4】:

      这是一个类似的问题。看看Adjusting time zone in PHP with DateTime / DateTimeZone

      有一个解决方案和一种更舒适的方式来完成您的任务。

      【讨论】:

        猜你喜欢
        • 2012-10-07
        • 1970-01-01
        • 1970-01-01
        • 2016-07-29
        • 2019-06-21
        • 2011-06-13
        • 2018-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多