how to convert time in AM /PM
Moderator: General Moderators
how to convert time in AM /PM
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
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
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.
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!?agtlewis wrote:Just subtract 12
Code: Select all
if ($hour > 12) { $hour - 12; $meridian = 'pm'; } else { $meridian = 'am'; }
Even shorter:
or maybe
Code: Select all
$hour = ($string == PM) ? $hour + 12 : $hour;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.
Correction to pickle's code:
Code: Select all
$hour += (trim(strtoupper($string)) == 'PM') ? 12 : 0;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.
Code: Select all
if($string=='PM'){Real programmers don't comment their code. If it was hard to write, it should be hard to understand.