Reputation: 365
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.
Upvotes: 0
Views: 766
Reputation: 48897
On the user login page, use javascript to change the form's action (or alternatively create a hidden field) that contains the user's UTC offset. The offset can be found using getTimezoneOffset
You can then store this offset in the user's session and/or table and use it to calculate the local time in PHP.
Upvotes: 1
Reputation: 1552
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:
Upvotes: 1