Nick
Nick

Reputation: 4462

How do I convert SQL Time records to seconds?

I am wanting to calculate the numbers of seconds for records stored in SQL Time format (e.g. 11:33:00) into the number of seconds from the start of the day (in this instance 41580, i.e. (11*60*60)+(33*60)) using PHP, so that I can add these seconds to a variable to calculate a DateTime. It appears that I can't use strtotime to do this. Do I need to write a function, and if so, could someone suggest an efficient function?

Upvotes: 0

Views: 92

Answers (1)

Gargron
Gargron

Reputation: 808

You are looking for the MySQL function TIME_TO_SEC, I think. It does exactly what you want. You would use it inside the SELECT statement like this:

SELECT TIME_TO_SEC(your_time_column) AS seconds FROM ...

Upvotes: 2

Related Questions