admin管理员组文章数量:1336118
First of all I apologize for asking yet another web related time & timezone question. I know there are a lot of resources and similar questions but I still can't understand how the time conversion between php and js time works.
So here is my case. I want to get the server time including timezone into javascript and do certain operations on the client side (which are not relevant to this question). I have this short snippet that does not work as I am expecting it to:
get_date.php
<?php
date_default_timezone_set('Romania/Bucharest');
echo date('D M d Y H:i:s');
?>
index.html
<body>
<!-- jQuery -->
<script src=".11.2/jquery.min.js"></script>
<script type='text/javascript'>
$.get( "get_date.php", function(data) {
current = new Date(data);
alert(current);
});
</script>
</body>
the alert message
So the problem is that I am getting the date and time for Romania (the location of the server), but the offset and timezone of Berlin (the location from where I am accessing the website). I would like to get the server time, date, offset and timezone correctly in Javascript, independent of the user location. For example the alert message should be Thu Feb 12 2015 13:26:10 GMT+0200(EET)
.
Also any clarifications of why this not work as expected are wele!
First of all I apologize for asking yet another web related time & timezone question. I know there are a lot of resources and similar questions but I still can't understand how the time conversion between php and js time works.
So here is my case. I want to get the server time including timezone into javascript and do certain operations on the client side (which are not relevant to this question). I have this short snippet that does not work as I am expecting it to:
get_date.php
<?php
date_default_timezone_set('Romania/Bucharest');
echo date('D M d Y H:i:s');
?>
index.html
<body>
<!-- jQuery -->
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type='text/javascript'>
$.get( "get_date.php", function(data) {
current = new Date(data);
alert(current);
});
</script>
</body>
the alert message
So the problem is that I am getting the date and time for Romania (the location of the server), but the offset and timezone of Berlin (the location from where I am accessing the website). I would like to get the server time, date, offset and timezone correctly in Javascript, independent of the user location. For example the alert message should be Thu Feb 12 2015 13:26:10 GMT+0200(EET)
.
Also any clarifications of why this not work as expected are wele!
Share asked Feb 12, 2015 at 11:45 skamsieskamsie 2,7266 gold badges41 silver badges50 bronze badges 05 Answers
Reset to default 5Change
get_date.php
<?php
date_default_timezone_set('Romania/Bucharest');
echo date('D M d Y H:i:s');
exit();
?>
And your Script file. Don't pass data to jquery's Date function.
<script type='text/javascript'>
$.get( "get_date.php", function(data) {
alert(data);
});
</script>
Please share the results.
Your browser will display the date and time depending on your system's time zone settings, when you use JavaScript's date functions. To prevent that you need to format the date on the server and handle it as a string. If for any reason you have to use the javaScript date functions, you need to correct your date by calculating the offset beforehand.
That is not possible using JavaScript’s native Date
object – that always operates in the client time zone.
Use a library like Moment.js and it’s extension Moment Timezone. Those allow you to work with any timezone you like in JS, including the possibility to parse dates and to format them.
You can use http://json-time.appspot./time.json or its variants like http://json-time.appspot./time.json?tz=GMT
var currTime;
$.ajax({
type: "GET",
dataType: 'jsonp',
url: "http://json-time.appspot./time.json",
async: false,
contentType: "application/json; charset=utf-8",
success: function (msg) {
console.log(msg);
// currTime.tz = "UTC"
// currTime.hour = 12
// currTime.datetime = "Thu, 12 Feb 2015 12:13:39 +0000"
// currTime.minute = 13
// currTime.second = 39
}
});
Here is a PHP
script which you can place on own server and use instead of json-time.appspot.
<?php
header('Content-Type: application/json');
header("Expires: Tue, 01 Jan 1990 00:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$error = "false";
$tz = $_GET['tz'];
if ( !in_array($tz, DateTimeZone::listIdentifiers())) {
$error = 'invalid time zone';
$tz = 'UTC';
}
date_default_timezone_set($tz);
?>
<?php echo htmlspecialchars($_GET['callback'], ENT_QUOTES, 'UTF-8' ); ?>({
"tz": "<?php echo $tz ?>",
"hour": <?php echo date('G'); ?>,
"datetime": "<?php echo date(DATE_RFC2822); ?>",
"second": <?php echo intval(date('s')); ?>,
"error": "<?php echo $error; ?>",
"minute": <?php echo intval(date('i')); ?>
})
use below code in PHP . i use code same in my project to get correct server time
<?php
date_default_timezone_set('Romania/Bucharest');
echo date('F d,Y h:i:s');
die();
?>
<body>
<!-- jQuery -->
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type='text/javascript'>
$.get( "get_date.php", function(data) {
current = new Date(data);
alert(current);
});
</script>
see w3school javascript data function to get correct date with string
本文标签:
版权声明:本文标题:javascript - How to display php server time and timezone correctly in JSjQuery independent of user location? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742403220a2468299.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论