My guess on (3) is that it’s a time zone issue, and/or that it’s based on the second that you run the script and you want the previous or next midnight. (Not quite sure how to fix either of those offhand).
Not sure if this’ll help but you can try
To get when they’re going to close, you’d need to add something to the SELECT part (which you can infer from the available fields) and change FROM like
FROM topics t, discourse_voting_topic_vote_count dvc, topic_timers tt
and add this after WHERE
AND tt.topic_id=t.id
(just like the one you added before).