Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sanitizing a Date

I am using a javascript date picker that allows the user to select a date. However, I would like to also sanitize the posted date data before entering into the database. I am not seeing any sanitize filter here: http://us2.php.net/manual/en/filter.filters.sanitize.php

What would be the best method to sanitize a date before entering into a database?

This would be the original value from the post:

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

Then I convert it for the database:

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

Thanks!

like image 726
MagentoMan Avatar asked Apr 13 '14 04:04

MagentoMan


People also ask

How do you sanitize data?

There are four primary methods to achieve data sanitization: physical destruction, data erasure, cryptographic erasure, and data masking.

What does it mean to sanitize data?

Data sanitization is the process of irreversibly removing or destroying data stored on a memory device (hard drives, flash memory / SSDs, mobile devices, CDs, and DVDs, etc.) or in hard copy form.

How do I sanitize my wordpress date?

If your date is like "03/02/2014" then you can simply clean your variable by regexp: $date = preg_replace("([^0-9/])", "", $_POST['date']);

What is the main purpose of a data sanitization tool?

The primary use of data sanitization is for the complete clearing of devices and destruction of all sensitive data once the storage device is no longer in use or is transferred to another Information system .


2 Answers

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 (/).

like image 149
Paul Denisevich Avatar answered Oct 03 '22 22:10

Paul Denisevich


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)
like image 23
ReverseEMF Avatar answered Oct 03 '22 22:10

ReverseEMF