views:

43

answers:

3

Hi Guys,

I am trying to create a script for quiz. I have stored current time stamp in mysql DB as quiz start time, and want to check after each quiz how much time is left.

I have the idea that I will add 30 mins to saved time stamp and subtract this value from current time. That will be the time left. But I don't know the exact way of doing this.

My time stamp is saved in DB in fromat 2010-08-24 20:08:59. Any one have the idea.

Please let me know if someone have done it, or know how to get it.

Adding 30 mins to time stamp and showing the user how much time is left.

I am using the now() function to store the timestmp in DB.

Thanks

+1  A: 

I would personally store the output of PHP time() in the database. If you a human readable format from this value, you could use date('Y-m-d H:i:s', $fromdatabase);.

Lekensteyn
A: 

With PHP you can just use strtotime('2010-08-24 20:08:59 +30 min') (see strtotime()).

But instead of saving dates in this format (by the way is this a datetime, text or varchar?) I would use unix timestamps (see time()), to which you can just add 1800 seconds (30 min). To get a human-readable date from that, use date().

Finally, in order to have a running timer displayed to the user, you'd need javascript.

NullUserException
Why not store it in the native [`DATETIME`](http://dev.mysql.com/doc/refman/5.0/en/datetime.html) format of MySQL? That way you can use the MySQL date time functions as well?
ircmaxell
@irc Yes, but as I suspected, the OP is using `VARCHAR`
NullUserException
Yes, I am using the VARCHAR for it already, and have the restriction not to change it.
kakaajee
@kakajee See the first line on my answer
NullUserException
here is my codeecho 'Start Time ::: ' . $begin_time = $s_time['start_time'];$start_time = strtotime("$begin_time+30 min");echo 'After 30 mins ::: '. date("Y-m-d H:i:s", $start_time);echo 'Current time ::: ' . $current_time = date( 'Y-m-d H:i:s' );$daysDiff = ($current_time - $start_time);echo $differ ='Differ by value:'.date("Y-m-d H:i:s", $daysDiff );and it does not return the correct value of difference.The "output is"Start Time ::: 2010-08-24 20:53:10After 30 mins ::: 2010-08-24 21:23:10Current time ::: 2010-08-24 17:38:00Differ by value:1929-05-10 03:10:20
kakaajee
I want to calculate the number of hours, mins and sec seperatly.Thanks for your help
kakaajee
@kakaa If my math doesn't fail me, "Start Time ::: 2010-08-24 20:53:10 After 30 mins ::: 2010-08-24 21:23:10 " looks about right; it's 30 min from the Start Time.
NullUserException
Thanks NullUserException for your reply. Your math is certainly good :),but at this point it does not calculate the correct time in hours, mins and sec, between the current time and the value got after adding 30 mins.I have done, the ajax part to show the values, but not getting the correct difference.Once again thanks for your help
kakaajee
+1  A: 

You want to store an actual UNIX timestamp in the database, not a string in that format.

You may or may not be doing this already, it depends on the type of column you're using. For MySQL, you should be using TIMESTAMP, which allows you to retrieve the timestamp with

SELECT UNIX_TIMESTAMP(column_name) ...

To store the current time + 30 minutes, all you have to do is:

INSERT INTO table (column_name) VALUES(UNIX_TIMESTAMP() + 1800)

You can know if the time has expired by comparing time() against the value of the column.

Artefacto
I have already a column there, with varchar type.
kakaajee