HTML/JS/PHP: Transform date entry into dd/mm/yyyy

Asked

Viewed 898 times

1

Good morning. In my mysql database the dates are saved as dd/mm/yyyy, but the "date" html input only returns dates in the yyyy-mm-dd format, so I can’t filter the database dates, which was my goal. I was using a very simple code, like this one below:

<input type="date"  name="parametro"> 
<input type="submit" value="Filtrar Data">

How to return the date in the same database format? In the case of plugins use, how to use them?

  • I suggest you take a look at Moment js. that does just that: https://momentjs.com/

  • You will compare the dates in the query, right? If possible, I advise you to store the dates in Y-m-d format to facilitate this comparison

  • Are you working with a database that already has formatted dates or are you entering these dates by the front-end entry? I ask this for your values already stored or that will be stored

  • The database already has dates formatted in dd/mm/yyyy. Use html to filter database data corresponding to the date inserted.

2 answers

1

With PHP it is simple to solve, just convert your base to input format

$dataBanco = "06/04/2018";
$data = date('Y-m-d', strtotime($dataBanco));
echo $data;
  • Thank you! Based on this code I managed to make it work.

0


Based on the answers I’ve been able to find a solution: As I needed to filter by date in the dd/mm/yyyy format, I used the following code in html and php:

HTML:

<input  type="date" placeholder= "dd/mm/aaaa" name="parametro" > 
<input type="submit" value="Filtrar Data"> 

PHP:

 $parametro = filter_input(INPUT_GET, "parametro");
 $data = date('d/m/Y', strtotime($parametro));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.