Monday, 2 February 2015

Twenty Four Hour to Twelve Hour

Twenty Four Hour to Twelve Hour

Recently I needed to convert a bunch of 24 hour times to 12 hours, the times were in different formats but I needed them in a format that was easily human readable. This function will convert a twenty four hour time to twelve hour time. Feel free to modify it to your own needs.

<?php
/**
 *
 * @convert 24 hour time to 12 hour
 *
 * @param string $time
 *
 * @return string
 *
 */
function twentyfourToTwelve($time)
{
    return 
date('g:i:sa'strtotime($time));
}


 
/*** Example usage ***/
 
echo twentyfourToTwelve('15:33:56').'<br />';

 echo 
twentyfourToTwelve('15:33').'<br />';

 echo 
twentyfourToTwelve('15.12').'<br />';

 echo 
twentyfourToTwelve('2:33:56').'<br />';

 echo 
twentyfourToTwelve('15:33').'<br />';?>

0 comments:

Post a Comment