Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • change time format from 24 hours to 12 hours

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 248
    Comment on it

    Following is the function to change time format from 24 hours to 12 hours, please take a look at input and output examples before you use it

    /*
     *  Return: 24 hours format to 12 hours format
     *  Input example: 1400, 730, 1205, 0
     *  Ouput example: 02:00PM, 07:30AM, 12:05PM, 12:00AM
     */
    function timeformat_24to12($timeval){
        $hours    = substr($timeval, 0, -2);
        $mins     = substr($timeval, -2);
        $meridian = 'AM';
    
        if($hours>12) {
            $hours -= 12;
            $meridian = 'PM';
        }else if($hours == 12 AND $min>0) {
            $meridian = 'PM';
        }else if($hours == 0) {
            $hours = 12;
        }
    
        # Prepend zero to hours and mins less than 9
        $hours = str_pad($hours, 2, '0', STR_PAD_LEFT);
        $mins  = str_pad($mins, 2, '0', STR_PAD_LEFT);
    
        return $hours . ':' . $mins . $meridian; // you may say I added noise here but readability got higher priority in my coding rules :P
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: