4

that the output of my code

i stored date using <input type="datetime-local"> in the database now i want to get back the date in <input type="datetime-local"> for update purpose.

i am doing

$query="select * from exam_schedule where subject_name='$name' AND class_id='$schedule_id' limit 1";
$result= mysqli_query($connection,$query);
while ($read_all_data = mysqli_fetch_assoc($result))
{ 

echo "date comming from datebase::".$date=$read_all_data['date_and_time']."<br>";
echo "duration comming from datebase::".$duration=$read_all_data['duration'];

$duration=$read_all_data['duration'];
echo "<form method='post' action='edit_schedule.php'>";
echo "<input type='hidden' name='id' value='$id'>";
echo  "<tr><th><input type='datetime-local' name='date' value='$date'> </th>";
echo  "<th><input type='datetime-local' name='duration' value='$duration'> </th>";
echo <input type='submit' name='update' value='update'> </form></th></tr>";
}

when i echo the $date and $duration it shows me values but when i put value="$date" it doesnot show me date from database.

22
  • provide the structure of your SQL table. Commented Oct 8, 2015 at 15:18
  • what do you see when you echo $date ? Commented Oct 8, 2015 at 15:19
  • 1
    what's "updation" supposed to represent? english.stackexchange.com/questions/68169/… Commented Oct 8, 2015 at 15:19
  • id type =INT date_and_time type=DATETIME duration type =DATETIME Commented Oct 8, 2015 at 15:20
  • 1
    you have a syntax error here echo "<th><input type='datetime-local' name='duration' value='$duration'> </th>"; <input type='submit' name='update' value='update'> </form></th></tr>"; and error reporting would've told you that. Missing an echo ". Commented Oct 8, 2015 at 15:22

4 Answers 4

7

The problem is that the datetime-local input type expects the date to be in this format: yyyy-mm-ddThh:mm but you are supplying it the default DATETIME format created by the database which is yyyy-mm-dd hh:mm:ss.

Demo

In order to change it, you need to change your SQL query to this:

SELECT *, DATE_FORMAT(date_and_time, '%Y-%m-%dT%H:%i') AS custom_date 
FROM exam_schedule 
WHERE subject_name='$name' 
AND class_id='$schedule_id' 
LMIT 1

so now in your results, you can then use the custom_date to prefill your input.

<input type='datetime-local' name='date' value='<?php echo $row['custom_date'] ?>' >
Sign up to request clarification or add additional context in comments.

Comments

3

If you need to convert the time in PHP instead of MySQL:

  1. Convert the MySQL datetime string into a PHP timestamp
  2. Apply a PHP date format mask to the PHP timestamp, where the format matches the HTML5 datetime-local format

PHP code sample:

const HTML_DATETIME_LOCAL = 'Y-m-d\TH:i'; //escape the literal T so it is not expanded to a timezone code
$php_timestamp = strtotime($mysql_date_string);
$html_datetime_string = date(HTML_DATETIME_LOCAL, $php_timestamp);
echo "<input type='datetime-local' name='date' value='{$html_datetime_string}'>";

2 Comments

This is not working for me: it is not showing in the input.
Updated. Thanks. Fixed $ for variable in string.
-1

Use This

 <?php  echo date('Y-m-d\TH:i:s', strtotime($exampledate));?>

Comments

-2

You can use this one:

date_format($date, 'Y-m-d H:i:s');

2 Comments

where can he use this code, why would they use this code, and how will it solve the problem?
it will format the date as 2013-03-15 00:00:00. Wherever you want date format like this, you can use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.