drkwpgrdb092239314 2014-07-21 21:37
浏览 159

从UTC转换时区

I'm having trouble converting timezone's from UTC to a user selected timezone. The problem seems to be Daylight Savings Time.

Here is an example I just coded.

<?php

date_default_timezone_set("UTC");

$timezone = -5.0;
$timestamp = time();

$local_time = $timezone * 3600 + $timestamp;

echo date( "m/d/Y - h:i A", $local_time );

?>

When I run the test file it returns 07/21/2014 - 04:29 PM. The current time is actually 5:29. The problem is Daylight Savings Time, where our clocks are turned back an hour.

How can I remedy this problem, or is there a more effective method for adjusting timestamps?

Easiest solution I see is making users select whether DST is currently in effect where they live, as not every country/timezone uses DST. If it is in effect then simply modifying the $timezone variable to +1 would suffice, but would require each user to manage whether DST is in effect or not.

Thank-you in advance.

EDIT:

I tried using DateTime but it was still off...

<?php

$timestamp = time();

echo 'Unix timestamp: ' . $timestamp . "<br/>";
echo 'Unix date: ' . date( "m/d/Y - h:i A", $timestamp ) . "<br/><br/>";

$dt = date_create_from_format('U', $timestamp);

date_timezone_set($dt, new DateTimeZone('America/New_York'));

$adjusted_timestamp = date_format($dt, 'U') + date_offset_get($dt);

echo 'America/New_York: ' . $adjusted_timestamp . "<br/>";
echo 'America/New_York: ' . date( "m/d/Y - h:i A", $adjusted_timestamp );

?>

The results were off by +2 hours. Returns 7:47 PM, it is currently 5:47 PM.

Unix timestamp: 1405979278
Unix date: 07/21/2014 - 11:47 PM

America/New_York: 1405964878
America/New_York: 07/21/2014 - 07:47 PM
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100