Sumit Kumar
Sumit Kumar

Reputation: 781

How can I display world time in javascript using php time() as a base time?

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?

Upvotes: 0

Views: 97

Answers (1)

hasan movahed
hasan movahed

Reputation: 364

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

Upvotes: 1

Related Questions