duanjing1276 2011-08-18 16:55
浏览 34
已采纳

向用户显示本地时间? PHP

I have this script which converts an array of 1-24 military time to AM/PM for the user to see. I want this script not only to display the hour and am/pm, but with the users local time. Can it be done?

Here is my script I am using to display the hour:

foreach($timelist as $time):
        echo "<input type='radio' name='time' value='$time' />";
        //echo $time;
        $hr = ($time <= 12 ? $time : ($time - 12));
        $ampm = ( (($time >= 12) and ($time < 24)) ? 'PM' : 'AM' );
        echo $hr." ".$ampm;
        echo "<br />";
endforeach;

I want $hr to display for example 12 GMT as the current users local time. So if they were in PST it would be 5 PST instead of 12 GMT because of the users location. Don't forget about AM/PM switching depending on location.

Any help is appreciated.

Thank You.

  • 写回答

2条回答 默认 最新

  • douting0585 2011-08-18 17:11
    关注

    The client doesn't send the timezone or local time in the request headers, so you can't do this in PHP alone without some outside help. There are a couple of ways to go about this:

    1. Get the local time on the client-side with Javascript, and "gracefully degrade" to the server time if the user has Javascript disabled. If this only needs to be displayed to the user, this is the easiest option. If you need to do something with that on the server-side, you could submit the timezone/time with an AJAX request.
    2. Use a geolocation API/package to guess the user's time zone based on their IP address. This is a lot more "expensive" time-wise and less accurate, but purely server-side. Use this to calculate the GMT +/- offset relative to the server time. You could then ask the user to confirm the calculated is correct on log-in.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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