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条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line