Date sanitization

I am using javascript date picker which allows user to select date. However, I would also like to misinform the published date data before entering the database. I do not see any cleaning filter: http://us2.php.net/manual/en/filter.filters.sanitize.php

What would be the best method for disinfecting the date before entering the database?

This will be the original value from the message:

$datepick = $_POST['date'];
// wich is 04/12/2014

Then I convert it to a database:

$date = date("Y-m-d", strtotime($datepick));

Thank!

+4
source share
3 answers

If your date is like "03/02/2014", you can just clear the variable with a regex:

$date = preg_replace("([^0-9/])", "", $_POST['date']);

This allows only digits (0-9) and the slash fwd (/) to be used.

+9

, :

  • , , , .
  • , FALSE.

:

DateTime::format
DateTimeImmutable::format
DateTimeInterface::format
date_format()
Date($format, $date_string)
+3

This expression can be used to support the formats 12/12/2016 and 12-12-1993.

filter_var (preg_replace("([^0-9/] | [^0-9-])","",htmlentities($input)));
0
source

Source: https://habr.com/ru/post/1536298/


All Articles