How to format "datetime -local" value in php fetched from mysql?

Viewed 2137

I have an input field in html where I want to echo the value of datetime stored in mysql.

<input type="datetime -local" name="schedule" value="<?php echo $result['schedule']; ?> 

But despite the value being there in mysql db as YY/mm/dd h:m:s the value is not showing up. What could be the possible issue? When I try to echo outside the input value it displays the result. My thoughts are that the datetime needs formatting according to the html datetime -local input field.

Please suggest me any way if possible.

3 Answers

T is a format character(Timezone abbreviation e.g: EST, MDT…) so you can't use it directly. Escape it (\T) to get a literal T character:

datetime.format.php

You can prevent a recognized character in the format string from being expanded by escaping it with a preceding backslash.

<?php
    $date = date("Y-m-d\TH:i:s", strtotime($result['schedule']));
?>
<input type="datetime-local" name="schedule" value="<?php echo $date; ?>"/> 

use <?=$date?> instead of <?php echo $date; ?>

You are filling it the wrong way. Read the documentation about the input-type datetime-local The correct format is YYYY-MM-DDTHH:MM - note the T as divider between date and time.

Also:

  1. you wrote the type wrong. Correct is datetime-local
  2. you are missing the closing quote at the value attribute.

This is how to fix it

<input type="datetime-local" name="schedule" value="<?php echo $date = date("Y-m-d\TH:i:s", strtotime($result['schedule']));?>">
Related