cjones26
cjones26

Reputation: 3814

Why can't I convert my 24 hour time to a 12 hour am/pm time with PHP?

I'm using Codeigniter and i'm having an issue where my 24 hour time column in a MySQL table is pulled by ActiveRecord & simply refuses to convert to a proper am/pm time.

In my DB table, I have a time stored as 22:00:00 (10:00 PM). When I pull the value from the database, the raw Unix time appears to be 1349960400 (which doesn't appear to be correct in itself) and when I run it through the date & strttotime functions it comes out as 22:00 PM.

This is what my code looks like:

function get_events_by_day($year, $month, $day)
{           
    $events = array();
    $query = $this->db->select('*')
                        ->from('calendar_events')
                        ->where('EventDate', "$year-$month-$day", 'after')
                        ->get();

    $events = $query->result(); 

    foreach($events as $event)
    {
        debug($event->EventStartTime); // prints 1349960400
        debug(strtotime($event->EventStartTime)); // prints 22:00
        $event->EventStartTime = date("H:i A", strtotime($event->EventStartTime));
        $event->EventEndTime = date("H:i A", strtotime($event->EventEndTime));
        debug($event->EventEndTime); // prints 22:00 PM
    }           

    return $events;
}

Any thoughts would be greatly appreciated.

Upvotes: 0

Views: 1511

Answers (1)

Green Black
Green Black

Reputation: 5084

date("h:i A", strtotime($event->EventEndTime));

Should solve it. H is 24 h, h = 12h.

Upvotes: 4

Related Questions