0

I need refresh my time zone every seconds.

I have this code:

$now = new DateTime(null, new DateTimeZone('America/New_York'));
$now->setTimezone(new DateTimeZone('Europe/London')); echo
$now->format("H:i:s A");

And I want to show time by time zone (state) on my server, with refresh.

Like this code but with my time zone state like Europe/London or other state:

<script type="text/javascript"> 
function display_c(){
    var refresh=1000; // Refresh rate in milli seconds
    mytime=setTimeout('display_ct()',refresh)
}

function display_ct() {
    var strcount
    var x = new Date()
    document.getElementById('ct').innerHTML = x;
    tt=display_c();
}
</script>


<body onload=display_ct();>
<span id='ct' ></span>

So is there code for JavaScript show DateTimeZone('America/New_York') with refresh time?

example : http://www.thetimenow.com/canada

i found this var java script but its wrong can any one plz corecet this code

    <script>
var server_name = "www.site.com";
var time_format = '24';

var timezones = ["America/Montreal","Etc/GMT+5","Etc/GMT+4","Etc/GMT+6","Etc/GMT+5","Etc/GMT+8","Etc/GMT+7","GMT","UTC"];
document.writeln(" $date = new DateTime($ref['lastupdated']); "); 
document.writeln(" $date->setTimezone(new DateTimeZone('America/Los_Angeles')); "); 

var current_time = {"America/Montreal":["<? print date("H", time())?>","<? print date("i", time())?>","<? print date("s", time())?>","<? print date("l", time())?>","<? print date("F", time())?>","<? print date("d", time())?>","<? print date("Y", time())?>"]]};


</script

the real code php is

<?Php

$date = new DateTime($ref['lastupdated']);
$date->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $date->format('m/d/y g:i:s a');
?>

update source in php

<?php
echo "<div id=\"output\"></div>";
echo "<script type='text/javascript'>
( function()
{ 
// use JavaScript Date to display current date
// in a div (#displayJsDate)
var output = document.getElementById('output');


var start = moment("<? print date("c")?>");
var delta = start - moment();

setInterval(function(){
    var now = moment().add(delta,'ms');
    var m = now.tz('$data[zone]');    // Set the time zone as desired
    var s = m.format('YYYY-MM-DD h:mm:ss A');
    output.innerHTML = s;
}, 1000);
})();
</script>";



?>
melado
  • 1
  • 6
  • 1
    You will need a js library, such as [moment-timezone](http://momentjs.com/timezone/). You could also just use [this site](http://www.timeanddate.com/clocks/free.html). Not sure what this has to do with PHP. – Matt Johnson-Pint Apr 20 '15 at 19:55
  • i have site that have time zone , so i wana code for all states and country to put them and just change the timezone name , so i wana code for java script like DateTimeZone('Europe/London – melado Apr 20 '15 at 22:05
  • Yes, you can do that with moment-timezone, or any of the libraries [listed here](http://stackoverflow.com/a/15171030/634824). – Matt Johnson-Pint Apr 20 '15 at 22:39

2 Answers2

0

You can create a recursive javascript function using setTimeout() to continuously update the current time. See this example

Walker Boh
  • 750
  • 6
  • 13
  • i know this , but how can i update the current time (real time on server) – melado Apr 20 '15 at 22:03
  • PHP gets the time based on the time zone set in your php.ini file. If there is no timezone set, it pulls it from the server's hardware time. Are you trying to set a hardware clock through PHP? Are you trying to update the time on a website? To understand what you're asking, you'll need to elaborate on why you need your server to constantly update the time. – Walker Boh Apr 20 '15 at 23:50
0

This has nothing to do with PHP. You need a JavaScript library that understands time zones.

For example, using moment-timezone, you can do something like this:

var output = document.getElementById("output");

var start = moment("2015-01-01T00:00:00Z"); // Current time UTC value from server
var delta = start - moment();

setInterval(function(){
    var now = moment().add(delta,'ms');
    var m = now.tz("America/New_York");    // Set the time zone as desired
    var s = m.format("YYYY-MM-DD h:mm:ss A");
    output.innerHTML = s;
}, 1000);
<script src="//momentjs.com/downloads/moment.min.js"></script>
<script src="//momentjs.com/downloads/moment-timezone-with-data-2010-2020.min.js"></script>
<div id="output"></div>

jsFiddle

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • thx u Matt Johnson , but the time in momentjs is not real becouse the time is show on computer clock not server time :( – melado Apr 20 '15 at 23:29
  • 1
    You need to refine your requirements then. Do you really want a call from the client to the server every second just to get the time? Or do you want to initialize an event on the client-side based on the time on the server side? And if so, how do you plan to handle DST transitions? Or is that not relevant in your case? – Matt Johnson-Pint Apr 20 '15 at 23:34
  • thx Matt Johnson for reply , look i have website for just time zone , so i see setTimezone from server its ok , becouse the time server is update the time every second and DST transitions its also update that , sorry if u dont understand becouse my english isnt so good :) – melado Apr 20 '15 at 23:50
  • I've updated the answer showing how you can initialize the time from another source. You could use PHP (or anything) to create that initial time value. – Matt Johnson-Pint Apr 21 '15 at 00:11
  • Just to be clear - you shouldn't use PHP to do time zone conversions for this particular purpose. If you did, the clock would not tick correctly as it crossed over a DST transition, until the user refreshed the page. The above code will indeed handle the DST transition properly. – Matt Johnson-Pint Apr 21 '15 at 00:14
  • hi Matt , i wana just ask u , can i use this code in php , becouse i use var start = moment(" print date("F d, Y H:i:s", time())?>"); in html and it work , but how can i use var start = moment(" print date("F d, Y H:i:s", time())?>"); in php , i hobe ur understand , and thx alot 4 u – melado Apr 21 '15 at 04:08
  • Any of these should work: `var start = moment(" print date("c")?>");` or `var start = moment.unix( print date("U")?>);` or `var start = moment( print time() * 1000 ?>);` or any other way you can match both sides. – Matt Johnson-Pint Apr 21 '15 at 04:17