0

<?php


$date = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9- ]/', ' ', urldecode(html_entity_decode(strip_tags($_POST['date']))))));
$date2 = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9- ]/', ' ', urldecode(html_entity_decode(strip_tags($_POST['date2']))))));

?>

  <!doctype html>
  <html lang="en">

  <head>
    <title>date</title>
  </head>

  <body>
    <form action="" method="post">

      <label for="message" class="form-label">Date:</label>
      <input type="date" class="form-control" id="date" name="date">
      <br>
      <label for="message" class="form-label">Date2:</label>
      <input type="date" class="form-control" id="date2" name="date2">

      <input id="saveForm" onclick="" class="button_text" type="submit" name="submit" value="Submit " />



    </form>

    <script>
      var date = new Date();

      var month = date.getMonth() + 1;
      var day = date.getDate();
      var year = date.getFullYear();

      if (month < 10) month = "0" + month;
      if (day < 10) day = "0" + day;


      var today = month + "-" + day + "-" + year;

      document.getElementById('date').value = today;

      console.log(today);


      //date works but output is diffrent

      var date = new Date();

      var month = date.getMonth() + 1;
      var day = date.getDate();
      var year = date.getFullYear();


      if (month < 10) month = "0" + month;
      if (day < 10) day = "0" + day;

      var today = year + "-" + month + "-" + day;

      document.getElementById('date2').value = today;

      console.log(today);
    </script>





  </html>

So the bottom one works when loaded and displays correctly however when submitted or console logged looks like this 2023-01-13 and when I change it to the correct format it stops working but will then console log correctly but will still echo out wrong... What is going on here? how can I make the date auto populate when page is loaded and still console log and echo out correctly like month/day/year ?????

```
<!doctype html>
<html lang="en">
<head>
<title>date</title>
</head>
<body>
<form action="" method="post">

<label for="message" class="form-label">Date:</label>
<input type="date" class="form-control" id="date" name="date">
<br>
<label for="message" class="form-label">Date2:</label>
<input type="date" class="form-control" id="date2" name="date2">

<input id="saveForm" onclick="" class="button_text" type="submit" name="submit" value="Submit "/>

                
                
</form>

    <script>


var date = new Date();

var month = date.getMonth() + 1;
var day = date.getDate();
var year = date.getFullYear();

if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;


var today = month + "-" + day + "-" + year;

document.getElementById('date').value = today;

console.log(today);


//date works but output is diffrent

var date = new Date();

var month = date.getMonth() + 1;
var day = date.getDate();
var year = date.getFullYear();


if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;

var today = year + "-" + month + "-" + day;

document.getElementById('date2').value = today;

console.log(today);

</script>
</html>
<?php
$date = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9- ]/', ' ', urldecode(html_entity_decode(strip_tags($_POST['date']))))));
$date2 = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9- ]/', ' ', urldecode(html_entity_decode(strip_tags($_POST['date2']))))));
echo '<hr><br>';
echo $date;
echo '<br>';
echo $date2;
?>
```

I assume when i change this it should output correctly but instead only makes it half work

      var today = month + "-" + day + "-" + year;
qwerty321
  • 1
  • 1
  • Post the code as a runnable snippet, it makes it much easier for others to help you. The value assigned to an input type date should be in the format YYYY-MM-DD, not MM-DD-YYYY per the first non–working example. – RobG Jan 13 '23 at 15:15
  • I added snipped but it wont let me echo out when submitted so how do i make it the way i want it then? – qwerty321 Jan 13 '23 at 15:25
  • Post the code that ends up in the page, not the PHP that generates it. To post as a runnable snippet, use the `<>` button when editing the post. – RobG Jan 13 '23 at 16:00
  • i WOULD REALLY LIKE TO JUST ASK A QUESTION AND GET AN ACTUAL ANSWER FOR IT – qwerty321 Jan 14 '23 at 02:46
  • I do not understand the problem you are facing. You don't need to do anything JS related with the dates you have - once you set them, check their values in the console, and upon posting. You will see that the values of `$date` and `$date2` are in `yyyy-mm-dd` format, same as they were before posting. If, however, you want to set the value of either of the dates to todays date, in `yyyy-mm-dd` format (the value, not the way it's displayed), see [this SO](https://stackoverflow.com/questions/23593052) and the related entries (answers, comments, etc). – FiddlingAway Jan 14 '23 at 13:34

0 Answers0