duan89197 2012-03-07 01:07
浏览 18
已采纳

每个用户的自动时区转换器

I'm running a comment script, and i want to fix the time each post was added in.

While it was posted 19:21 in norway it were posted 20:21 somewhere else. Something witch also makes a "time since" useless.

Do you understand the issue?

I'm basically i'm looking for a way to change the time to the local area of each user.

Could anyone provide me with a solution for my problem? I'm currently using php, but i could go for javascript too.

I've seen a website use this code: document.write( GetElapsedTime( 1330160527 ) ); But then again, i have no idea of how i could get each time-value.

  • 写回答

3条回答 默认 最新

  • dongzuo4666 2012-03-07 01:29
    关注

    In Javascript, new Date().getTime() returns the number of milliseconds since midnight Jan 1, 1970 local time, that is, based on the browser's time zone. To convert this to the server's time, the server must render the page with its own calculation of milliseconds elapsed since Jan 1, 1970, the you can use Javascript to convert the browser's time to the server's timezone, like this:

    var serverTime = 1330160527; // Sever's time rendered by PHP
    // Calculate time difference, rounding it to nearest half hour.
    var timeDifference = Math.round ( (new Date().getTime() - 1330160527) 
             / (30 * 60 * 1000) ) * (30 * 60 * 1000); 
    
    // Later, when we post the time to the server, we adjust by timeDifference
    var localTime = (new Date()).setTime(new Date().getTime() - timeDifference);
    

    Note that we round the time difference (in milliseconds) to the nearest half-hour because two different timezones must be at least half an hour apart, but never a smaller fraction of an hour.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊