I have database user consist of attribute like name,password,dob and etc. datatype is used to store date is date.while updating, I used to fetch like below
$query="select * from user where email='$user'";
$res=mysqli_query($conn,$query);
$fet=mysqli_fetch_array($res);
<input type="date" id="dob" name="dob" value="<?php if($fet['dob']==00-00-0000){ }else{ echo date($fet['dob']);} ?>">
I want date which is to fetch from database to this above input type date.
EDIT:
As I understood in comments of my answer, you are storing DATETIME
values in your database and not DATE
values. Here is how to get a date format from a datetime format using DateTime
class:
$dob = new DateTime($fet['dob']);
$formatted = $dob->format('Y-m-d');
To output it in short syntax in your <input>
:
<?php
$dob = (new DateTime($fet['dob']))->format('Y-m-d');
?>
<input type="date" id="dob" name="dob" value="<?php echo $dob; ?>">
Issues:
An <input type="date">
should have a value in the YYYY-MM-DD
format.
The date()
function expects a format and a timestamp.
You did not quote the 00-00-0000
value.
You won't need to format your date string as its format is right for your input:
<input type="date" id="dob" name="dob" value="<?php echo $fet['dob']; ?>">
If you need to manipulate dates in PHP I would recommend the DateTime
class.
This should be a comment, but its a bit long.
I used to fetch like below
What you've shown is not valid PHP - it won't work. PHP will tell you when stuff is not working and why if your error logging/reporting is configured correctly.
input type="date"
I presume you are aware this has very limited support in browsers
select *
Generally its a very bad idea not to be specific about what you are selecting from a database, and particularly so in this scenario. Even if the underlying data type for the dob is a date, you should do an explicit format on it to get it into the right form for the later processing (in this case, injecting it as the value of the input)