RussellHarrower
RussellHarrower

Reputation: 6820

PHP turn PM and AM into 24 hour

So I need away to in PHP turn pm / am into 24 hour time, We use Yahoo API to get currency rates, but they seem to give it to us by am/pm.

I thought date might work, but when I read into that function it would had no way to turning pm into 24 hours.

5: 55pm

Upvotes: 2

Views: 2178

Answers (3)

CrayonViolent
CrayonViolent

Reputation: 32537

problem with using date and strtotime is you may have to worry about timezone... what you are getting from yahoo may not line up with your server's timezone...here is a regex method:

$time = preg_replace_callback('~^([^:]+)(.*(a|p)m)$~',create_function('$m','return ($m[3]=="p")?(($m[1]+12).$m[2]):$m[0];'),$time);

Upvotes: 0

mpen
mpen

Reputation: 283313

If the Yahoo! API really does not provide a way to return the time in 24 hours, you could do as sachleen suggested, or use a regex.

(\d{1,2}): (\d{2})(am|pm)

Then just add 12 to the hours if it's "pm".

Upvotes: 0

sachleen
sachleen

Reputation: 31141

This should work

date("H:i", strtotime("5:55 pm"));

and if you want to go the other way,

date("g:i a", strtotime("17:55"));

Upvotes: 8

Related Questions