Fix issue when seconds is greater than two digits.

Also only use one query rather than a loop.