i have this input which i take like this
<input type="datetime-local">
which i am inserting in the db successfully. In the database it stores as such in my table
2021-05-17 23:00:00
but then i am trying to display it in my page like so
<input type="datetime-local" value="<?php echo $date_time; ?>" >
but it gives me a blank field
but when i inspect it shows my the correct value in the value field
get datetime from database using DATE_FORMAT()
SELECT *, DATE_FORMAT(datetime, '%Y-%m-%dT%H:%i') AS custom_date
and after fetching data in which variable your data exist use that in your input value
<input type='datetime-local' name='datetime' value='<?php echo $row['custom_date'] ?>' >
Related
I've created a form so I can update a database table. Everything's working (text, file, select, radio) except my date form fields.
Empty, they show the default date placeholder text: mm/dd/yyyy. I can insert a value manually:
<input name="iu_received" type="date" class="form-control" id="iu_received" value="2022-09-20">
And that will show as 09/20/2022 on the page.
However, trying to insert a value from my database recordset doesn't do anything and the placeholder text still displays. For example:
<label class="form-label mb-0">Date Distributed:</label> <input type="date" class="form-control" name="iu_distrib" value="<?php echo $rsAppl->getColumnVal("iu_distrib"); ?>">
(Yes, the "iu_distrib" value is a mysql date: 2022-09-20.)
How can I make this work?
In my code, here is how i convert html date field values to unix timestamp before inserting to MySQL db which works fine:
//HTML Code:
<div class="form-group">
From:
<input type="date" name="dateFrom" value="<?php echo date('Y-m-d'); ?>" required/>
</div>
<div class="form-group">
To:
<input type="date" name="dateTo" value="<?php echo date('Y-m-d'); ?>" required/>
</div>
//PHP Code to convert to Time Stamp:
<?php $post->unix_stamp = strtotime($_POST['dateFrom']);
$post->unix_stamp_ex = strtotime($_POST['dateTo']);
$post->save(); ?>
How do i convert the unix timestamp back to html input date value for editing? What i've tried:
<input type="date" name="dateTo" value="<?php echo date("Y-m-d\TH:i:s",$post->unix_stamp_ex); ?>" required/>
Where $post->unix_stamp_ex is the unix timestamp value. (doesn't work for me, shows dd/mm/yyyy instead). My problem is how to get the underlying value, to display in the date input so users can edit it
The reason your input is showing a placeholder 'dd/mm/yyyy' is because it cannot read the format you gave it. It can't work with time, so you need to remove it from your code:
value="<?php echo date('Y-m-d',$post->unix_stamp_ex); ?>"
This will show the date in the input.
Also, mind that the displayed value will differ from the actual value format, as per my comment and RoussKS' answer.
According to Mozilla Developer docs for input type date
The displayed date format will differ from the actual value — the displayed date is formatted based on the locale of the user's browser, but the parsed value is always formatted yyyy-mm-dd.
[edit]
Just noticed the comment response, was typing as El_Vanja commented
[/edit]
Brother, I am able to convert the database string back to readable date time using the following method.
Solutions
$databaseTime = '2020-03-22 09:45:48';
$date = strtotime($databaseTime);
echo "DatabaseTime: " . $databaseTime . "<br>";
echo "Date in epoch: ".$date."<br>";
echo "Readable date: ".date("Y-m-d H:i:s",$date)."<br>";
Results
https://prnt.sc/rkmleq
I am building a web page for an admin dashboard using PHP and I have the following code, for the input date form and what I want is that if the "startdatum" is empty it must fill today's date in automatically.
<input type="date" class="form-control" placeholder="startdatum" id="startdatum" name="startdatum">
Here's how you can do it:
Btw, the <input type="date"> isn't supported on safari.
This example assumes that the input tag will always be empty when showing up, the user can later change the values.
<?php
$a = date("Y-m-d");
?>
<input type="date" id="startdatum" name="startdatum" value="<?php echo $a ?>">
I have a datatype "TIMESTAMP" in database. And I pass the value by using "datetime-local" attribute in html.
<input type="datetime-local" id="start_time" class="form-control" name="start_time"/>
this works fine and i can upload the date and time to database. But when i retrieve the data form database for editing, it doesn't work.
PHP Code
$query="SELECT * FROM hire WHERE hire_id='$id'";
$query_run=mysqli_query($con,$query);
$row=mysqli_fetch_array($query_run);
HTML Code
<input type="datetime-local" value="<?php echo $row['start_time']; ?>" />
The above php within the html shows nothing as a result. What is the fault here?
Because you have the input type set to 'datetime-local'. You have to have the date set to the right format in order for it to show up in the input.
Change your start_time value to be in this format.
$date = date("Y-m-d\TH:i:s", strtotime($row['start_time']));
Then in your input field echo out $date
<input type="datetime-local" value="<?php echo $date; ?>" />
first of all if you should get date from mysql then convert that string to date using strtotime function after that change the format of date and set it at value attribute of input form control haivng type="datetime-local".
$string_to_date=$d=strtotime($date_fromdatabase);
$new_date=Date('Y-m-d\TH:i',$string_to_date);
set as a value attribute of input element in laravel 6
<input type="datetime-local" class="form-control" value="{{ $new_date }}">
I want to auto get auto date value from database to html form on datepicker, I tried this one but getting 01/01/1970 instead of that date stored in database.
<input name="last_date" type="date" value="<?php echo strftime('%Y-%m-%d', strtotime($query[lastdate]->startdate)); ?>" />
while when I try , <? echo $query['lastdate']; ?> it showing correct date from database