0

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.

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

My problem is that the table that I would like to place the timer in is generated by MySQL. By the way, I wanted a real-time countdown timer which is capable of counting down every seconds and showing it to the user at the same time. I have no problems in getting the time difference but I'm not sure how to initialize the Javascript timer in the table.
1

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.

6 Comments

How do I call the Javasscript function from the Mysql table? I'm using a while loop together with mysql_fetch_array. The rows are populated by using $row[column]
I wanted a dedicated column just for the countdown.
the jQuery countdown plugin requires you to have a valid JavaScript date object. In order to get this you need pull a mysql date or timestamp object from mysql and insert it into a webpage where javascript can read it. $endTimeFromDb; was what I was calling that in my example. You create a new JavaScript date with that object and send it to your plugin. Again, this can be a string or a timestamp, though a timestamp might be easier. You probably don't want to store your timer in the database, just the end time good luck!
I tried using var endTime = <?= $row[end_time]; ?>; but its not working. no results were shown.
is this up on a url somewhere? is it in a <script> tag? is $row[end_time] even valid? Shouldn't it be $row["end_time"]. What about a print_r($row)? I'm sure we can get it working soon.
|

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.