0

Hello Everyone,

I have table name schedule in my database. There is field sch_time in that. sch_time stores time in GMT. I also have timestmp field which stores data in unix format.

The time stored in sch_time is of future date i.e. schedule of some event.

This events are displayed on homepage through php + smarty functions.

Can anyone let me know how I can display timing according to user's timezone.

E.g. If user is from India, time should be Gmt + 05:30.

2
Contributors
11
Replies
13
Views
7 Years
Discussion Span
Last Post by phphit
0

Hi phphit

I would suggest outputting the date as normal to the webpage.
Then use javascript to convert it on their home computer.
This will use a lot less code and save a headache
Read the javascript tutorial at http://www.w3schools.com

Hope this helps

0

I am not having knowledge of php or javascript. Only thing I can do is edit as per my need.

So, can anyone guide me to url some is already using. Or can someone put the code here.

0

I would highly recommend you read some basic tutorials
http://www.w3schools.com
read the php tutorial and javascript tutorial.
It will only take half an hour to read and you'll get a much better understanding of coding

0

I have uploaded image file, which will make you known what I want.

Currently there is GMT time stored (in both HH:MM and Unix timestmp way).

As I am right now in India (IST) it should display users time zone (IST) and calculate timing according to visitor's local time (+05:30)

I think we will need to use something mix up of php and javascript

Attachments time.PNG 6.2 KB
0

you can use the php server response to get the ip address of the computer that sent the request, which you can then send back the time in their time.
However it is much simpler to first output the time to the client, then have javascript determine which country it is in.
(If you want to you can send along the servers date then use javascript to find the difference between that date and the clients clock)
it will say -5 or +3.5(3 & 1/2 hours)
If I have time later I can make a script up but am fairly busy atm.

Hope this helps :)

0

you can use the php server response to get the ip address of the computer that sent the request, which you can then send back the time in their time.
However it is much simpler to first output the time to the client, then have javascript determine which country it is in.
(If you want to you can send along the servers date then use javascript to find the difference between that date and the clients clock)
it will say -5 or +3.5(3 & 1/2 hours)
If I have time later I can make a script up but am fairly busy atm.

Hope this helps :)

I am able to find time difference. But have no idea how to integrate with PHP + Smarty.

Moreover, Another problem is of detecting time zone. I mean if person is from India, it should display IST.

Anyway, whenever you get upto script, do reply here.

0

I am close to something,

Hope some can help me.

With below code I can print GMT. Now, tell me how I integrate javascript to show user time too.

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("mysql2", $con);
$currentdate = date("Y-m-d");  
$result = mysql_query("SELECT * FROM schedule WHERE sch_date >= '$currentdate' ORDER BY timestmp ASC limit 20");

echo "<table border='1'>
<tr>
<th>GMT</th>
<th>Your Time Zone</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['sch_time'] . "</td>";
  echo "<td>" . $row['sch_time'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

As of now it shows GMT time in both field. Now, help me to integrate javascript to make it working.

0
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("mysql2", $con);
$reminder ="<script type="text/javascript">var d = new Date();var gmtHours = -d.getTimezoneOffset()*60;document.write(+ gmtHours);</script>";
$currentdate = date("Y-m-d");  
$res = mysql_query("SELECT * FROM schedule WHERE sch_date >= '$currentdate' ORDER BY timestmp ASC limit 20");
while ( $row = mysql_fetch_array($res) ) {
   echo '<br />'.date('H:i', strtotime("+$reminder seconds",strtotime($row["sch_time"]))); 
}

?>

But It looks javascript part is not working. When I replace javascript part with seconds it works like a charm.

0

sorry im not a master with javascript but try using a function that simply prints the date of the server, then compares with the system
i.e:
server time - client time = difference
print that time.
you can then post that to the server to use elsewhere if you need to.

0

Although I got the code correctly it's not working.

<script type="text/javascript">
function GetIst() { 
var d = new Date()
var gmtHours = -d.getTimezoneOffset();
document.write(+ gmtHours);
}
</script>

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("mysql2", $con);
$reminder ="<script language=javascript>GetIst();</script>";
$currentdate = date("Y-m-d");  
$res = mysql_query("SELECT * FROM schedule WHERE sch_date >= '$currentdate' ORDER BY timestmp ASC limit 20");
while ( $row = mysql_fetch_array($res) ) {
   echo '<br />'.date('H:i', strtotime("+$reminder minutes",strtotime($row["sch_time"]))); 
}

?>

When I execute below code, it does works.

<?php 
$time_current = "<script language=javascript>GetIst();</script>"; 
echo "". $time_current; 
?>

Any Idea ?

Error from above code I am getting is that all time are shown as 00:00. I think some issue is with strtotime

Edited by phphit: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.