it is possible to convert express time in 1h22m14s or 22m55s To the format 01:22:14, 00:22:55 in an easy way using php?
Asked
Active
Viewed 326 times
0
-
1http://php.net/manual/en/datetime.createfromformat.php – Hossam Apr 19 '17 at 17:08
1 Answers
3
Yes, create a format string, the use the DateTime
class to parse from the format using the method createFromFormat
. It's really a magical class!
PHP.net has excellent documentation on this: http://php.net/manual/en/datetime.createfromformat.php
They even have an example close to your specific question you'd just need to tweak it a bit:
$mytime = '23h15m03s';
echo DateTime::createFromFormat('H\hi\ms\s',$mytime)->format('H:i:s');
If you have a part the drops off occasionally (like hours in your example) you'll need to play with the format to get it to work.

Ray
- 40,256
- 21
- 101
- 138
-
1@webDev looks like it will work for that case, but how would you handle the example: `22m55s`. You'd need some additional logic (`strtotime()` would see it as `hh:mm` and you'd get 22:55:00 not 00:22:55) – Ray Apr 19 '17 at 17:43
-
You are right @Ray, I did not think about that, simple tweak i have done, would you please see and let me know whether that will work or not, thank you very mush – BetaDev Apr 19 '17 at 17:54
-
Sure it will work, though I'd never recommend it! IF you're going the regular expression route, you could probably figure a single expression to cover both cases. – Ray Apr 19 '17 at 17:56
-
now i can understand why you dont recommend, yeah that will not good in every situation. there might be 22h or 56s only too. you can delete my solution. I understood and thank you – BetaDev Apr 19 '17 at 17:58
-
1@webDev ok, I'll comment it out. You should take a swing at creating the regex--it would be a good learning experience. If you find it, add it as an answer! – Ray Apr 19 '17 at 18:00