working with a chat box where getting the msg time with raw as far I've got this $msg_time = strtotime($row["date_time"]);
which is showing the strtotime values correctly. now getting confused how to show this with local timezone
Asked
Active
Viewed 462 times
3

Rimi Khan
- 83
- 1
- 4
-
You need to display server or client timezone? – Leandro Papasidero Feb 22 '15 at 09:18
1 Answers
1
Server Timezone
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
echo date("m-d-Y", strtotime('2014-02-21')) . "timezone: " . date_default_timezone_get();
Client Timezone
file where you want to display the timezone
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Title</title>
<!-- javascript/jQuery -->
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<?php
session_start();
echo isset($_SESSION['time']) ? $_SESSION['time'] : "error";
?>
</head>
<body>
<script type="text/javascript">
$(document).ready(function () {
var visitortime = new Date();
var visitortimezone = "GMT " + -visitortime.getTimezoneOffset() / 60;
$.ajax({
type: "POST",
url: "timezone.php?XDEBUG_SESSION_START=1",
data: 'time=' + visitortimezone,
success: function (data) {
}
});
});
</script>
</body>
</html>
timezone.php
<?php
session_start();
$_SESSION['time'] = $_POST['time'];

Leandro Papasidero
- 3,728
- 1
- 18
- 33
-
what if the local timezone is America/Vancouver is this correct? date_default_timezone_set('America/Vancouver'); – Rimi Khan Feb 22 '15 at 09:21
-
And I dont want to echo it, I want to convert it as $msg_time how can I do this? – Rimi Khan Feb 22 '15 at 09:22
-
-
Still ambiguous. what are you want to do with the timezone? – Leandro Papasidero Feb 22 '15 at 09:26
-
`elseif($_POST["fetch"]==1) { $results = mysqli_query($sql_con,"SELECT user, message, date_time FROM (select * from shout_box ORDER BY id DESC LIMIT 50) shout_box ORDER BY shout_box.id ASC"); while($row = mysqli_fetch_array($results)) { date_default_timezone_set('America/Vancouver'); $msg_time = date('h:i A M d',strtotime($row["date_time"])); echo ''.$row["user"].''; }`I want to show the time in my localtime zone instead of server time – Rimi Khan Feb 22 '15 at 09:31
-
ahhh you are talking about client's time zone. you have to use Javascript. Remeber PHP is server side and JS client side, please read this old question that explain exactly what are you trying to do: http://stackoverflow.com/questions/4746249/get-user-timezone – Leandro Papasidero Feb 22 '15 at 09:34
-
-
Did you read the link? if you did and you follow the instructions you should display $_SESSION['time']; But you need to follow the instruction. Otherwise, you are going to get blank – Leandro Papasidero Feb 22 '15 at 09:40
-
-
this is the script http://dnetbd.com/shout_php.txt where It's happening, please tell me how can I implement your solution with my script. – Rimi Khan Feb 22 '15 at 10:13
-