Page 1 of 1
how to convert time in AM /PM
Posted: Fri Feb 10, 2006 4:56 am
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
Posted: Fri Feb 10, 2006 5:11 am
by dude81
Got the answer myself
Code: Select all
function hourconverter ($hour, $string){
if($string=='PM'){
$hour=$hour+12;
}
return $hour;
}
Posted: Fri Feb 10, 2006 5:11 am
by Benjamin
Just subtract 12
Code: Select all
if ($hour > 12) {
$hour - 12;
$meridian = 'pm';
} else {
$meridian = 'am';
}
Posted: Fri Feb 10, 2006 5:12 am
by dude81
thanks agtlewis
Posted: Fri Feb 10, 2006 5:12 am
by Benjamin
Oh ok, I misread your question I thought you were trying to do the opposite.
Posted: Fri Feb 10, 2006 5:13 am
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!?
Posted: Fri Feb 10, 2006 5:15 am
by Benjamin
Yes lol I need to go to bed. At least the code is right though.
Posted: Fri Feb 10, 2006 11:16 am
by pickle
Even shorter:
Code: Select all
$hour = ($string == PM) ? $hour + 12 : $hour;
or maybe
Code: Select all
$hour += ($string == PM) ? 12 : 0;
Posted: Fri Feb 10, 2006 2:45 pm
by josh
Correction to pickle's code:
Code: Select all
$hour += (trim(strtoupper($string)) == 'PM') ? 12 : 0;
Posted: Fri Feb 10, 2006 3:15 pm
by pickle
Well, ~dude81's code showed:
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.