-1

I have code witch will give me the timestamp for New York;

echo "<p>America/New_York</p>";

date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");

What I want to achieve is the seconds to be counting up after the time is loaded.

This is saved in a file which has the usual html head and body tags but it saved with .php extension. Wondering if I need some sort of a timeout function.

worldofjr
  • 3,868
  • 8
  • 37
  • 49
HattrickNZ
  • 4,373
  • 15
  • 54
  • 98
  • 3
    You'll need to use Javascript. There are a million tutorials for that. – rjdown Dec 13 '15 at 20:15
  • 7
    [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – John Conde Dec 13 '15 at 20:15
  • 1
    well, you could add some script to that, like the question is if you care about performance (if no, then it's ok, if yes, then you have to do some more javascripting) and if the client has javascript on – Kondziutek Dec 13 '15 at 20:20
  • @Kondziutek got a working example [here](http://jsfiddle.net/nKUwn/147/) with help from [here](http://stackoverflow.com/questions/18793520/how-to-use-moment-js-for-a-certain-timezone-and-display-it-in-real-time) but this is probably the poor perfromance method you refer to. can you advise how an alternative method? – HattrickNZ Dec 14 '15 at 23:15

1 Answers1

0

Php is a server-side language. And so this returns the time in the specified format.

What your looking for is javascript. You could solve this with this code:

function setTime() {
var d = new Date(),
  el = document.getElementById("time");

  el.innerHTML = formatAMPM(d);

setTimeout(setTime, 1000);
}

function formatAMPM(date) {
  var hours = date.getHours(),
    minutes = date.getMinutes(),
    seconds = date.getSeconds(),
    ampm = hours >= 12 ? 'pm' : 'am';
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  minutes = minutes < 10 ? '0'+minutes : minutes;
  var strTime = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
  return strTime;
}

setTime();

I'm assuming your html looks likes this:

<p>
  America/New_York
</p>
<p>
  the time is <span id='time'></span>
</p>

Working Jsfiddle: https://jsfiddle.net/z53byjcp/1/

Adapted AMPM function from this SO post: How do you display javascript datetime in 12 hour AM/PM format?

Edit: This is indeed local time. I recommend you look into MomentJS for the timezones. http://momentjs.com/timezone/

Community
  • 1
  • 1
yeouuu
  • 1,863
  • 3
  • 15
  • 32
  • This shows local time and you plagarized this from another answer without attribution – rjdown Dec 13 '15 at 20:53
  • I copied the AMPM function partially from the SO mentioned above (edited). Also added a reference to momentjs since they do an excellent job for timezone related code. – yeouuu Dec 13 '15 at 20:58
  • i asked the question as I was hoping to be able to use this php function `date_default_timezone_set` to get the different time zones. Bu I am struggling to get this way to work. So I am exploring using moment.js but I am having difficulties with the timezone part of that, but there is a good working example [here](http://stackoverflow.com/questions/18793520/how-to-use-moment-js-for-a-certain-timezone-and-display-it-in-real-time) to hopefully get me there. – HattrickNZ Dec 14 '15 at 22:24
  • If you need any help. Don't hesitate to ask. – yeouuu Dec 15 '15 at 10:32