format_date() 不适用于时区
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你解决的方法是正确的。如果您使用 PHP 5.3 或更高版本,您可以使用 DateTime::add方法并简单地添加偏移量,而不像我下面那样从中创建时间戳。
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.