I am making a timer function that runs in MySQL, and it sets start_time with MySQL CURRENT_TIMESTAMP when they start the timer.
When they stop the timer, I calculate in the SQL the time difference, and add it on to time, and set start_time to 0 again.
UPDATE `time`
SET `time` = time + (UNIX_TIMESTAMP(CURRENT_TIMESTAMP)-UNIX_TIMESTAMP(start_time)),
`start_time` = '0000-00-00 00:00:00'
WHERE `id` = '1'
When I stop the timer, instead of using the start_time in the query, it sets it to 0000-00-00 00:00:00 first, so my time is actually (time + (UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP('0000-00-00 00:00:00'))), which is not the correct value.
CREATE TABLE IF NOT EXISTS `time` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`time` double NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`start_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
How can I fix this?
Why not do it in two separate updates?
Look at your schema. The default value is set to that and the update statement is using the current value of it. Are you changing the value of start timer before hand? Try adding a select before your update to see what the value of start_time is.
I just tried this..
update time
set start_time = ‘2011-08-28 17:02:47’
where id = 1;
UPDATE
timeSET
time= (time + (UNIX_TIMESTAMP(CURRENT_TIMESTAMP) – UNIX_TIMESTAMP(start_time))),start_time= ‘0000-00-00 00:00:00’WHERE
id= ‘1’;and id = 1 got a correct number in time. Check to make sure start_time and time have good values.