format_date() 不适用于时区

发布于 2024-12-10 10:21:40 字数 1494 浏览 0 评论 0原文

  • Drupal 7.8 安装
  • 站点时区在区域设置中设置为 America/New_York
  • 我在页面回调中有此代码
  • 问题发生在多个服务器上

format_date() 不会根据默认站点时区调整时区偏移,甚至当我添加时区字符串作为参数。

下面是代码,代码底部是注释掉的输出。有 2 个使用 format_date 的示例,最后一个示例是我必须做的才能显示正确的时间。

关于如何让 format_date() 使用时区有什么想法吗?

  header('content-type: text/plain');

  // utc_str as it would come from the db of a date field
  $utc_str = '2011-09-01 14:00:00';
  // converting to a unix timestamp
  $timestamp = strtotime($utc_str);

  // first print with format_date, note default site timezone is America/New_York
  print 'format_date($timestamp, "custom", "Y-m-d h:s:i"): '. format_date($timestamp, 'custom', 'Y-m-d h:s:i') ."\n";

  // next print date by actually setting the timezone string in the argument
  // Result:
  $tz_str = 'America/New_York';
  print 'format_date($timestamp, "custom", "Y-m-d h:s:i", "America/NewYork"): '. format_date($timestamp, 'custom', 'Y-m-d h:s:i', $tz_str) ."\n";

  // this is the only way i could get it working
  $date = new DateTime($product->field_class_date['und'][0]['value'], new DateTimeZone(date_default_timezone_get()));
  $offset = $date->getOffset();
  $formatted = date('Y-m-d h:s:i', ($timestamp + $offset));
  print $formatted;

  /** This is the output

    format_date($timestamp, "custom", "Y-m-d h:s:i"): 2011-09-01 02:00:00
    format_date($timestamp, "custom", "Y-m-d h:s:i", "America/NewYork"): 2011-09-01 02:00:00
    2011-09-01 10:00:00
  */
  • Drupal 7.8 install
  • Site Timezone set to America/New_York in the region settings
  • I have this code in a page callback
  • Problem happens on multiple servers

format_date() is not adjusting for the timezone offset by either the default site timezone, or even when I add the timezone string as an argument.

Below is the code, and at the bottom of the code is the output commented out. There are 2 examples using format_date, and the last example is what I had to do to get the correct time to display.

Any ideas on how to get format_date() working with the timezone?

  header('content-type: text/plain');

  // utc_str as it would come from the db of a date field
  $utc_str = '2011-09-01 14:00:00';
  // converting to a unix timestamp
  $timestamp = strtotime($utc_str);

  // first print with format_date, note default site timezone is America/New_York
  print 'format_date($timestamp, "custom", "Y-m-d h:s:i"): '. format_date($timestamp, 'custom', 'Y-m-d h:s:i') ."\n";

  // next print date by actually setting the timezone string in the argument
  // Result:
  $tz_str = 'America/New_York';
  print 'format_date($timestamp, "custom", "Y-m-d h:s:i", "America/NewYork"): '. format_date($timestamp, 'custom', 'Y-m-d h:s:i', $tz_str) ."\n";

  // this is the only way i could get it working
  $date = new DateTime($product->field_class_date['und'][0]['value'], new DateTimeZone(date_default_timezone_get()));
  $offset = $date->getOffset();
  $formatted = date('Y-m-d h:s:i', ($timestamp + $offset));
  print $formatted;

  /** This is the output

    format_date($timestamp, "custom", "Y-m-d h:s:i"): 2011-09-01 02:00:00
    format_date($timestamp, "custom", "Y-m-d h:s:i", "America/NewYork"): 2011-09-01 02:00:00
    2011-09-01 10:00:00
  */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‖放下 2024-12-17 10:21:40

你解决的方法是正确的。如果您使用 PHP 5.3 或更高版本,您可以使用 DateTime::add方法并简单地添加偏移量,而不像我下面那样从中创建时间戳。

$utcTimezone = new DateTimeZone('UTC');
$timezone = new DateTimeZone('America/New_York');
$dateTime = new DateTime('2011-09-01 14:00:00', $timezone);
$offset = $timezone->getOffset($dateTime);
print date('Y-m-d H:i:s', $dateTime->format('U') + $offset);

The way you have solved it is correct. If you use PHP 5.3 or higher you can use the DateTime::add method and simply add the offset, without making a timestamp from it like i did below.

$utcTimezone = new DateTimeZone('UTC');
$timezone = new DateTimeZone('America/New_York');
$dateTime = new DateTime('2011-09-01 14:00:00', $timezone);
$offset = $timezone->getOffset($dateTime);
print date('Y-m-d H:i:s', $dateTime->format('U') + $offset);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文