How can I get 2 digits in a time that I get from mySQL with PHP?

I am using this code for get only the time from a timestamp in mySQL with PHP.

$timestamp = strtotime($row['timestampSQL']);
$time = date('G:i:s', $timestamp);

and this shows 3:00, would be so difficult to show 03:00?

Thanks again, Leandro.

2 answers

  • answered 2018-10-11 19:42 Felippe Duarte

    AS you can see in the docs:

    G 24-hour format of an hour without leading zeros 0 through 23

    H 24-hour format of an hour with leading zeros 00 through 23

    So, replace:

    $time = date('G:i:s', $timestamp);


    $time = date('H:i:s', $timestamp);

  • answered 2018-10-11 19:52 Mateus Felipe

    You are using G: i: s

    but should use H: i: s to convert its time to 0X if the result of H is less than 10

    here is an example

    $query = $this->db->query("SELECT '2018-10-11 03:48:22' AS now");
    $date = $query->row['now'];
    $time = date('H:i:s', strtotime($date));

    the result is: 03:48:22