The HTML datetime-local control does not include a timezone. Validate its exact format first, then attach the timezone that your application expects.
$value = $_POST['input_time'] ?? '';
$date = DateTimeImmutable::createFromFormat('!Y-m-d\TH:i', $value, new DateTimeZone('Asia/Kolkata'));
$errors = DateTimeImmutable::getLastErrors();
if ($date !== false && ($errors === false || ($errors['warning_count'] === 0 && $errors['error_count'] === 0))) {
echo $date->format('Y-m-d H:i:s P');
}This current example appears first; the detailed examples below are retained and modernized for comparison and additional use cases.
<input type='datetime-local' id='input_time' name='input_time'
value='2018-06-12T19:30' min='2018-06-07T00:00' max='2018-06-14T00:00'>
<input type='datetime-local' id='input_time' name='input_time'
value='2018-06-12T19:30:46'
min='2018-06-07T00:00:56' max='2018-06-14T00:00:34' step=1>
//$dt=$_POST['dt']; // Getting data from POST method of form
$dt="2019-02-16T16:56:49";
$date = DateTime::createFromFormat('Y-m-d\TH:i:s', $dt);
if($date){$msg=' Date object is created using the input ';}
else{$msg=' Unable to create date object ';}
echo $msg;
createFromFormat() to create date and time object
$date = new DateTime(); // Date object using current date and time
$dt= $date->format('Y-m-d\TH:i:s');
echo "<input type='datetime-local' id='input_time' name='input_time' value='$dt'>";
Output is here : $dt_min = date_create('2018-12-31 23:15:40'); // Minimum limit
$dt_min= $dt_min->format('Y-m-d\TH:i:s');
$dt_max = date_create('2015-05-31 20:14:45'); // Maximum limit
$dt_max= $dt_max->format('Y-m-d\TH:i:s');
$dt = new DateTime(); // Date object using current date and time
$dt= $dt->format('Y-m-d\TH:i:s');
echo "<input type='datetime-local' id='input_time' name='input_time' value='$dt' min='$dt_min' max='$dt_max'>"; Output is here
$dt_min = new DateTime('-1 month -10 days');
$dt_min= $dt_min->format('Y-m-d\TH:i:s');
$dt_max = new DateTime('+1 month 10 days'); // Maximum limit
$dt_max= $dt_max->format('Y-m-d\TH:i:s');
$dt = new DateTime(); // Date object using current date and time
$dt= $dt->format('Y-m-d\TH:i:s');
echo "<input type='datetime-local' id='input_time' name='input_time' value='$dt' min='$dt_min' max='$dt_max'>";
Output is here : Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.