How can PHP developers ensure that the datetime-local input field displays the correct date and time values when editing data from a database?

When editing data from a database, PHP developers can ensure that the datetime-local input field displays the correct date and time values by fetching the datetime value from the database, formatting it in the correct format for the input field, and then setting it as the value attribute of the input field.

<?php
// Fetch datetime value from the database
$datetime_from_db = "2022-01-01 12:00:00";

// Format the datetime value for the datetime-local input field
$formatted_datetime = date('Y-m-d\TH:i', strtotime($datetime_from_db));

// Set the formatted datetime value as the value attribute of the input field
echo '<input type="datetime-local" name="datetime" value="' . $formatted_datetime . '">';
?>