Sanitizing a Date

柔情痞子 提交于 2019-12-03 16:14:22

If your date is like "03/02/2014" then you can simply clean your variable by regexp:

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

This allows only digits (0-9) and fwd slash (/).

Formatting the date sanitizes it, because:

  1. If the formatter succeeds, then it will only be a date, with syntax controlled by the format string.
  2. If it fails, then FALSE is returned.

This is true of:

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

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

filter_var (preg_replace("([^0-9/] | [^0-9-])","",htmlentities($input)));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!