1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Looking for a time function....

Discussion in 'PHP' started by blackburn2413, Nov 4, 2010.

  1. #1
    Hello everyone, looking for a function to use to display time properly. I have multiple times stored as "$time" in the format of XXXX. Its always a 4 digit number in military time from 0001 to 2400. Is there a function to display these time properly in the format of 11:45pm instead of 1145?

    I could go the totally newb way and use a huge amount of if statements for the times I need (IF $time = 0600 then $newtime = 6:00am) but I swear there is a function I am overlooking....

    Any ideas?
     
    blackburn2413, Nov 4, 2010 IP
  2. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    //

    you could always just set $hour to the first two, $minute to the last two, then if $hour > 12, $hour - 12 and set $ante = 'pm';
     
    krsix, Nov 4, 2010 IP
  3. sabaina

    sabaina Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try this. Just wrote this, might have errors, but you'll get the idea.

     
    sabaina, Nov 4, 2010 IP
  4. bencummins

    bencummins Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It works, but made a couple of changes.. if the time is in 24 hr format, and you're then converting it to have am/pm on the end.. you want it in 12 hours format

    
    function convert_time($time)
    {
            //for example, $time = "0600";
            $a = substr($time, 0, 2);
            $b = substr($time, 2, 4);
    
            $am_or_pm = (int)$a >= 12 ? "pm" : "am";
    
            if ($a> 12) $a-=12;
    
            $new_time = sprintf("%02d:%02d%s",$a,$b, $am_or_pm);
    
            return $new_time;
    }
    
    
    
    PHP:
     
    bencummins, Nov 5, 2010 IP
  5. sabaina

    sabaina Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    great! glad i could be of help.
     
    sabaina, Nov 5, 2010 IP