0

How can I display time of different countries using javascript? I'm trying to use PHP (America/New_York) to get a baseTime, pass it to javascript and then display different time for respective countries.

<?php
print '
<script type="text/javascript">
var baseTime = "'. time(). '";

//javascript functions
function australiaTime(){

// what do I do here to convert baseTime into australia time?

}//function

function usaTime(){  }

function IndiaTime(){ }

function hongkongTime(){  }

function londonTime(){  }

function japanTime(){ }



</script>
';
?>

I was looking at some world time examples, but they all use client side PC time to do whatever they are doing. What if PC clock is not correctly set? What can I do here?

4
  • Have you searched any answers on the internet first? Maybe you should now try to reinvent the wheel, but check for other examples. Surely someone have tried this before, since it sounds really common. Could check this out to see if it is what you want proglogic.com/code/javascript/time/worldclock.php Commented Aug 30, 2016 at 8:26
  • You could just skip the php tag around the script, and then just have the script and do var baseTime = '<?=time()?>' Commented Aug 30, 2016 at 8:29
  • You do have to get the time and timezone from a certain place. Using PHP you could get your server's time, provided that it is indeed correct(being a server, it's time should be updated) Commented Aug 30, 2016 at 8:29
  • if you have already found some examples, why not pass the php time into them instead of the Client side pc time ? Commented Aug 30, 2016 at 8:34

1 Answer 1

1

you can use library momentjs: http://momentjs.com/timezone/

example :

var newYork    = moment.tz("<?=date("Y-m-d h:i") ?>", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london     = newYork.clone().tz("Europe/London");

newYork.format();    // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format();     // 2014-06-01T17:00:00+01:00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.