Reputation: 141
I have a MYSQL table and I would like to place a countdown column for each result in the table. The timer should count from the current time to the the column ending time(a column within the same table, in DATETIME format)
How can I do that? I refer to the Javascript timer from this site http://keith-wood.name/countdown.html however I have no idea how to make it countdown to the ending time column and also echo the Javscript timer in the rows of the table.
Upvotes: 0
Views: 3358
Reputation: 17014
Just an FYI, you can write a counter purely in javascript on the client. If you want to countdown to a specific time in the future that's stored in a mysql database you can still use Javascript and do something like this in your PHP page:
<script>
var endTime = <?= $endTimeFromDb; ?>; // preferably a unix timestamp in ms
var relativeTime = new Date().getTime() - endTime; // relative ms until endTime
// now apply some fancy formatting and write it to the dom
</script>
Then you can use something like TimeSpan.js to convert your relativeTime into a pretty format: https://github.com/idottv/TimeSpan.js
See the MDN for more information about the JavaScript Date Object: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
After Looking more careful at the plugin you want to you use I came up with this
var endTime = <?= $endTimeFromDb; ?>; // if it's a timestamp
var endTime = "<?= $endTimeFromDb; ?>"; // if it's a string
$("div.countdown").countdown({until: new Date(endTime)});
Let me know if neither of those examples made any sense.
Upvotes: 1
Reputation: 91734
I think you are mixing up server-side and client-side although I'm not completely sure what you are trying to do.
You don't need any timers in your database, you just need the end-time. In your php script you can then get the current time (or in MySQL...) and the end-time and initialize your javascript timer with the difference between the two so that the timer can count down in the visitor's browser.
Upvotes: 1
Reputation: 7991
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
You wouldn't store the countdown column directly. Instead you would use timediff.
So something like
select timediff(now(), end_time) from countdown_table
Upvotes: 2