-2

All the "date" in my database are of the following format: 'd-m-Y' (E.g. 09-03-2022)

I want have a field with input type=date, so user can modify the date.

I use the following code to pull data:

$date      = "<input type=\"date\" id=\"date\" size= \"50%;\" name=\"date\" value=\"$res[date]\" >";

...
<thead> <th>Date </th> <td>$date </td> </thead>

But my inputbox is showing dd/mm/yyyy instead of the actual date.

enter image description here

What formatting do I need to do to make my actual date show on the inputbox?

I have tried the following but doesn't work:

$date      = "<input type=\"date\" id=\"date\" size= \"50%;\" name=\"date\" value=\"date('d-m-Y',$res[date])\" >";
user2741620
  • 305
  • 2
  • 7
  • 21
  • 1
    What is the result of the code you've posted? The first one (the second won't work since you can't put PHP functions in strings like that.) You need to format the date before adding it, and why not format it in the same format as it expects (using / instead of -)? Right click on the page and then "view page source". Side note: _"All the "date" in my database are of the following format: 'd-m-Y' (E.g. 09-03-2022)"_ - That's not a good way of storing dates. When storing dates, set the column as a "date" type in the database, which expects the dates to be in the format `Y-m-d`. – M. Eriksson Jan 03 '23 at 11:29
  • Why are you storing the HTML of the "input" field in a variable...? You can just concatenate your date from the database with HTML like that ` Date ` – Juan Jan 03 '23 at 11:37
  • 1
    The value of a date input has to be in YYYY-MM-DD format (or empty) – brombeer Jan 03 '23 at 11:44
  • 2
    `All the "date" in my database are of the following format: 'd-m-Y' (E.g. 09-03-2022)`...why are you storing dates as text in your database?? That's a bad design mistake. Use a DATE or DATETIME column type, that's what it's there for. Dates are not strings (except when you're showing them to humans or serialising them for transmission). Your current storage format makes it impossible to properly compare, sort, or filter the database rows by date, and - as you've found - more difficult to extract the date info in a useful way for usage elsewhere. – ADyson Jan 03 '23 at 11:52
  • 1
    Anyway https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date makes it quite clear that the `value` attribute must be a date serialised into YYYY-MM-DD format. It's unclear why you tried to use d-m-Y therefore, or why you don't know what format to use. Always read the manual. – ADyson Jan 03 '23 at 11:53
  • The "date" values are stored into the database using the formula $date = date('d-m-Y'); When I look at the SQL tables, the date fields look like 09-03-2022, for one example. So when I SELECT from database and display the date, it looks like 09-03-2022. But how do I display it on the "input type=date" field? The input field is showing me dd/mm/yyyy... Not 09/03/2022 – user2741620 Jan 03 '23 at 13:32
  • @Mohammad Salehi answered your questions it seems to me in his answer no...? Even if your dates are not stored correctly you can do a query and use date_format it seems to me. – Juan Jan 03 '23 at 15:34

2 Answers2

0

You are passing a wrong format to your date input. Date input must fill with RFC3339 formatted text, you can read more about it.

To change your current format to Y-m-d format, you can use below code:

DateTime::createFromFormat('d-m-Y', $dateInput)->format('Y-m-d');

If you are using MySQL, it uses YYYY-MM-DD format for date type column, so, saving the date with Y-m-d from PHP to MySQL date field seems to be the correct way.

Mohammad Salehi
  • 565
  • 1
  • 12
  • 33
  • Thanks. After much testing, I was able to get the following code to work: $date1 = date_create("$res[date]"); $date2 = date_format($date1,"Y-m-d"); $date = ""; – user2741620 Jan 04 '23 at 02:13
0

So I believe the reason why the date is not displaying on my input field is becuz it is in String datatype and need to format to DATE.

$date1  =   date_create("$res[date]");
$date2  =   date_format($date1,"Y-m-d");
$date   = "<input type=\"date\" id=\"date\" size= \"50%;\" name=\"date\" value=\"$date2\" >";

With this, I can also do the same for TIME

$time1  =   date_create("$res[time]");
$time2  =   date_format($time,"H:i:s");
$time   = "<input type=\"time\" id=\"time\" size= \"50%;\" name=\"time\" value=\"$time2\" >";
user2741620
  • 305
  • 2
  • 7
  • 21
  • Mark your answer as 'accepted answered' if it solved your problem. – Mohammad Salehi Jan 04 '23 at 08:26
  • 1
    You answer your question "believing that the reason is that" but these are indications that many of us gave in the comments and @MohammadSalehi in his answer, no...? – Juan Jan 04 '23 at 09:02