1

I have the following php code that returns time in different time zone:

<?php
date_default_timezone_set('America/New_York'); 
echo (new DateTime())->format('r');
?>

and that works fine, when I run it in the browser I get the time:

Tue, 29 Sep 2015 12:07:01 -0400

Now, I have a jquery script that calls ajax this php page and displays it on my page. It starts like this:

$.ajax({
    type: 'GET', 
    url: 'timeinnewyork.php',
    complete: function(resp){

        var today;
        today = new Date(resp.responseText);
        alert(resp.responseText);
        alert(today);
        (...)

and the first alert returns the time in new york:

Tue, 29 Sep 2015 12:07:27 -0400

but the second one shows me my current european timezone:

Tue Sep 29 2015 18:07:27 GMT+0200 (Central Europe Daylight Time)

I want to use the New York time and not my local time. How can I fix that?

randomuser1
  • 2,733
  • 6
  • 32
  • 68

1 Answers1

0

Return a UTC time from PHP and then format in JS using library like http://momentjs.com/timezone/

orlland
  • 1,256
  • 8
  • 12
  • I think he wants to use the timezone that the server returns. – Barmar Sep 29 '15 at 16:23
  • His perspective might be wrong. It's better if the server will be neutral to timezones especially if you are serving to different timezones. If you are right, he could easily pass the timezone from server to JS. – orlland Sep 29 '15 at 16:27
  • @Orland, could you give me some example of how to use properly moment.js here? I want to create a datetime object, so that I can use it later on, for example: `var mm = today.getMonth();` – randomuser1 Sep 29 '15 at 16:27
  • I think that's already beyond the purpose of SO. You could also refer to this: http://stackoverflow.com/questions/15347589/moment-js-format-date-in-a-specific-timezone – orlland Sep 29 '15 at 16:31