Search code examples
phphtmlhtml-input

How to get "AM/PM" in data read from a form input (type=time)?


I have this code:

<input class="meting" name="meetingtime" value="<?php echo $_POST['meetingtime'] ?>" type="time"/>

<?php
    $time = $_POST['meetingtime'];
    echo $time; // the output of this is only the numbers
?>

How can I get the AM/PM values in PHP?


Solution

  • Oct 2022

    PHP has a build in function to do that.

    $time = $_POST['meetingtime'];
    echo date("g:i a", $time);
    // 10:22 AM
    

    Fatal error?

    If you get a fatal error try adding strtotime() to the time.

    Fatal error: Uncaught TypeError: date(): Argument #2 ($timestamp) must be of type ?int...
    
    $time = $_POST['meetingtime'];
    echo date("g:i a", strtotime($time));
    // 10:22 AM