how to convert time in AM /PM

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

how to convert time in AM /PM

Post by dude81 »

Hello
Are there any PHP functions to convert the time in AM and PM to 24 hour format i.e
2.30 PM to 14:30 and so? Can any one help. I have browsed php.net for functions I didnt get a right one for AM and PM
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

Got the answer myself

Code: Select all

function hourconverter ($hour, $string){
if($string=='PM'){
$hour=$hour+12;
}
return $hour;
}
Last edited by dude81 on Fri Feb 10, 2006 6:23 am, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Just subtract 12

Code: Select all

if ($hour > 12) {
  $hour - 12;
  $meridian = 'pm';
} else {
  $meridian = 'am';
}
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

thanks agtlewis
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Oh ok, I misread your question I thought you were trying to do the opposite.
Last edited by Benjamin on Fri Feb 10, 2006 5:15 am, edited 1 time in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

agtlewis wrote:Just subtract 12

Code: Select all

if ($hour > 12) {
  $hour - 12;
  $meridian = 'pm';
} else {
  $meridian = 'am';
}
Isn't that the opposite of what he wants to do!?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Yes lol I need to go to bed. At least the code is right though.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Even shorter:

Code: Select all

$hour = ($string == PM) ? $hour  + 12 : $hour;
or maybe

Code: Select all

$hour  += ($string == PM) ? 12 : 0;
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Correction to pickle's code:

Code: Select all

$hour  += (trim(strtoupper($string)) == 'PM') ? 12 : 0;
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Well, ~dude81's code showed:

Code: Select all

if($string=='PM'){
So I assumed $string would be "AM" or "PM". You're correct thought that your code would ensure $string didn't have whitespace and matched exactly.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply