Changing date input value with PHP
I have this input:
<input type="date" name="datin" value="">
I need to change the value of this input as the value of $ row ['eventoDatIn'], which is also a date in YYYY-MM-DD format.
I tried to repeat the value in three ways:
<input type="date" name="datin" value="
<?php
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo date($row['eventoDatIn']);
}
}
?>">
It:
<input type="date" name="datin" value="
<?php
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo date(htmlspecialchars ($row['eventoDatIn']));
}
}
?>">
And this:
<input type="date" name="datin" value="
<?php
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo (htmlspecialchars ($row['eventoDatIn']));
}
}
?>">
Input always ends, showing the default mm / dd / yyyy. Is this a problem with the date format?
source to share
No problem in your encoding, you missed the first parameter of the function date
which is format specifier
as well as the second parameter value in second
, try below code: -
<input type="date" name="datin" value="
<?php
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo date('Y-m-d', strtotime($row['eventoDatIn'])); // check the change
}
}
?>">
source to share
The first parameter of the function is actually the date format. Characters that are not valid in the format are printed as is. date
So basically you're echoing the current time in a format that doesn't contain valid formatting options, so the result is "format" (which is actually your date) literally.
Try
echo date('Y-M-d', $row['eventoDatIn']);
PS: You said, "I have a date in this format", but I hope the date is a real date (timestamp) in the database. If it's a formatted string, you might have to convert it to a timestamp first. If possible, do this in a database and store your dates and times as actual dates and times, not strings.
In MySQL, you can convert dates to UNIX_TIMESTAMPS that PHP can read:
SELECT UNIX_TIMESTAMP(YourDateField) AS TheDate FROM YourTable
source to share