View Full Version : Automatically Converting Date and Time by Timezone

11-03-2006, 10:33 PM
I have been searching the Internet for hours trying to find a way to convert the local server time to a user's local time. I have seen quite a few ways of doing it. I am wondering if there was a simple way. I already know of the "adding/subtracting x hours to local time" based upon the GMT offset. However, that does not compensate for users in timezones in daylight savings time. In addition, this does not compensate for the date change, that is, if the server time is 2359 on Jan 1, a user in a timezone two hours ahead would actually be 0159 Jan 2.

Any help would be appreciated.

11-04-2006, 04:16 PM
compensating for the date change.
if you know the offset:

date("dM'y H:i", strtotime("+2 Hours"));

11-04-2006, 04:44 PM
Here is some code that I did based on the FAQ example (sending variables from javascript to PHP) in the PHP manual. This sends the visitors date to the PHP page and could be modified to send any value that is available -

if (isset($_GET['umm']) AND isset($_GET['udd'])) {
// output the value gotten from the browser as a test
echo "Your MM: {$_GET['umm']} Your DD: {$_GET['udd']}<br />\n";
} else {
// form and pass the date variables

echo "<script language='javascript'>\n";
echo "var d=new Date()\n";
echo "var dd=d.getDate()\n";
echo "var mm=d.getMonth()\n";
echo " location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
. "&umm=\" + mm + \"&udd=\" + dd;\n";
echo "</script>\n";
?> Also, here is a link to a post that basically sends the server's time to the browser and lets it calculate and use the difference between it and the visitor's time - http://www.codingforums.com/showthread.php?t=98742

11-05-2006, 11:49 PM
I have a similar problem, although SQL is involved. I store a variable called date ($date) with other info. via a form

When I query the database,
$query = "SELECT rain,f_name,l_name,property, town, date FROM $table ORDER BY date DESC, rain";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);

As I try to output this to a table, there is a time lage between my local time and server time.

echo "<td class='header'>".date("d-m-Y", strtotime($row['date']))."</td>\n";

How can I modify this to refelect my local time?
BTW - date is kind of a default value - I didn't get them to add it, just to save time in form entry. Should I make them add the date - would that make the process easier?