2

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.com/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

enter image description here

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 welcome!

skamsie
  • 2,614
  • 5
  • 36
  • 48

5 Answers5

5

Change

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.

Vineet
  • 4,525
  • 3
  • 23
  • 42
  • This will only display the date and time without the offset and timezone information. Also I need to create a Date object in javascript for further manipulation, having just a string displayed does not help. – skamsie Feb 12 '15 at 12:01
  • a) You can use [date("e")](http://php.net/manual/de/function.date.php) for adding the timezone on the server-side generated string, but b) for further manipulation of the date in JS this will have no effect. Please see also http://stackoverflow.com/questions/20834411/specify-timezone-in-javascript – agoldev Feb 12 '15 at 12:13
0

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.

Burki
  • 1,188
  • 19
  • 28
0

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.

CBroe
  • 91,630
  • 14
  • 92
  • 150
0

You can use http://json-time.appspot.com/time.json or its variants like http://json-time.appspot.com/time.json?tz=GMT

var currTime;
$.ajax({
            type: "GET", 
            dataType: 'jsonp', 
            url: "http://json-time.appspot.com/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.com

<?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')); ?>
})
void
  • 36,090
  • 8
  • 62
  • 107
  • Thanks for the suggestion, but how would I adapt the script on the client side to get the desired result? – skamsie Feb 12 '15 at 12:11
0

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.com/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

Nishit Maheta
  • 6,021
  • 3
  • 17
  • 32