How do you validate input time using preg_match method

I want to store time in a 12:25:30 format into the data base. But how can I validate the user input data using preg_match method. All search results I found come with time and date and the format like 10:50 AM, but I want time in 24 hour (00:00:00) format to use in further calculation. I know how to calculate such as 10:20:30 + 5:50:40 = 16:11:10.

How do I achieve this.


An easy way would be:

preg_match('/\\d{2}\\:\\d{2}\\:\\d{2}/', $_POST['time']);

Later on you could make it more complex, that’s just two digits followed by a colon, followed by 2 digits, another colon and another two digits. You could make it be valid number within, ie - Minutes field could never be higher than 60, etc.

Thanks for replay, I’ve tested with the snippet bellow and it passed. I’ve no idea if it right or wrong.

if(!preg_match('/^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/', $_POST[time']))