View Full Version : Timestamp convert to local date
DigitalBliss
04-18-2005, 10:09 PM
Hey I have timestamps in my database, I wanted the time to print in the users local time zone. For instance if the actual time read 5:08 EST it would say 2:08 PST. How can this be done?
marek_mar
04-18-2005, 10:28 PM
You ask the users for their timezone and add/substract as many hours as needed from GMT (which you can get using gmdate() (http://www.php.net/gmdate) or gmmktime() (http://www.php.net/gmmktime)).
DigitalBliss
04-18-2005, 10:43 PM
im sorry but I dont quite know how to use that, right now the code I have is
$mp_date = strftime("%a %m-%d-%Y %I:%M %p", $mp_date );
DigitalBliss
04-18-2005, 11:37 PM
anyone anyone?
Velox Letum
04-19-2005, 01:04 AM
Here is a simple php script I coded to display times in a select box for users to select the correct time with. Set the correct server offset and they should be able to just select the correct time from the dropdown box...no worries about DST.
<?php
// Velox Letum (2005)
// elementation@gmail.com
// http://www.nanoshock.net
$GMT_offset = "-5"; // Set server offset from GMT
$GMT_time = time() - $GMT_offset*3600;
$i = -11;
echo "<select name='TZ'>";
while ($i <= 11) {
if ($i > 0) {
$TZ_num = "+" . $i;
} else {
$TZ_num = $i;
}
$TZ_time[$i] = $GMT_time-$i*-3600;
if ($i == 0) {
echo "<option value='$TZ_num' selected='selected'>" . date("h:i:s A", $TZ_time[$i]) . " ($TZ_num)</option>";
} else {
echo "<option value='$TZ_num'>" . date("h:i:s A", $TZ_time[$i]) . " ($TZ_num)</option>";
}
$i++;
}
echo "</select>";
?>
DigitalBliss
04-19-2005, 01:19 AM
Yea I am using that script to let the users determine their time zone. But I need a script that will change all times to their time zone. I am using a timestamp to record the times. Is there another way to do this?
Velox Letum
04-19-2005, 01:37 AM
This is the reason the value is $TZ_num, so that it can be saved and retrieved for a user. The $TZ_num is the offset for which you can set your user's clocks by. Example:
<?php
// Insert MySQL login/database select
$GMT_offset = "-5"; // Server offset...of course I think there is a GMT time function...
$time = mysql_query("SELECT tz_offset FROM users WHERE id='$_SESSION['uid']'");
$time = mysql_fetch_array($time);
$time = time() - $time['tz_offset']*-3600 - $GMT_offset*3600;
echo "Time now is " . date("h:i:s A", $time);
?>
$time['tz_offset'] is the $TZ_num. Demo for a Denver/Mountain timezone (-7): http://area51.nanoshock.net/date2.php
DigitalBliss
04-19-2005, 01:49 AM
WHat happens if the time in the timestamp is not gmt? Is there a way I can use the time(); function and still get the gmt time?
Velox Letum
04-19-2005, 01:53 AM
The timestamp is server time, which is usually not GMT, thus the $GMT_offset variable. My server is on the east coast of the United States in the Eastern Time Zone, so it is -5.
DigitalBliss
04-19-2005, 03:06 AM
Okay I got everything to work, thanks a lot for your help again Velox Letum
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.