I have multiple rows with time
"42 sec"
"1 min"
"2 h 32 min"
Is it possible to convert this to
"00:00:42"
"00:01:00"
"02:32:00"
with php?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
PHP’s
strtotime()is a useful function that can convert a string representation of a time into a unix timestamp. From that we can then convert the time into any format we like.However, your original time strings aren’t in a format that
strtotime()can handle directly. eg. ‘h’ must be ‘hour’. But we could perhaps replace these before passing to strtotime() if your data is consistent.Note we attempt to convert the original time relative to 0, not the current time.
Will output:
Note that
strtotime()understands ‘sec’, ‘second’, ‘min’, ‘minute’ and ‘hour’. And appears to handle space or no space OK eg. ’32min’ or ’32 min’ is handled OK.