0

(I've had to edit this post since John Conde wrongly marked it as answered because he didn't understand my question.)

In my PHP app, the user should be able to enter a date in any format the strtotime() function understands. Here is the input:

    <label>Release Date:</label>
    <input type="text" name="release_date"/>&nbsp;<span>Use any valid 
           date format</span>
    <br />

Then the index page draws it from the form and stores it in the database like this...

$release_date = $_POST['release_date'];

and displays it on the list page like this:

<td><?php $date = date('n/j/Y', strtotime($product['releaseDate'])); 
                      echo $date; ?></td>

I know how to convert dates from one format to another. The display side is fine. But WHERE and HOW do I take the user input - entered in ANY date format the strtotime() function can interpret - and turn it into a readable format going into the database? Right now if I enter any date not in 'xxxx-xx-xx' format it's read as 0 and comes out as UNIX 0, 1/1/1970.

Thank you.

Q Rice
  • 13
  • 3
  • 1
    How do you know whether 1/2/2014 means January 2nd or 1st February? – johannes Feb 12 '16 at 17:54
  • You can't have a universal interpreter of date format. How can you identifier is '1/2/2016' is january 2nd or february 1st? – fusion3k Feb 12 '16 at 17:54
  • 1
    See PHP's amazing DateTime class.http://php.net/manual/en/class.datetime.php – jjwdesign Feb 12 '16 at 17:56
  • the instruction to the user says "enter any valid date format". is there no way to distinguish a valid from an invalid date format in php? – Q Rice Feb 12 '16 at 17:56
  • @jjwdesign it's the same with DateTime. OP can use createFromFormat , but also in this case he has to known the schema... – fusion3k Feb 12 '16 at 17:58
  • from what i'm studying right now, I think the book wants me to use the strtotime() parser, and there is a specific set of date formats that it understands. i just don't know where and how to use it. – Q Rice Feb 12 '16 at 18:08

1 Answers1

1

Some dates would work find, and you can do it, but some dates such as anything from the 1st to the 12th of the month would be ambiguous since you could not tell the difference between the month and the day. If you allow two digit years you have an issue with year's 2001 to 2012 as well - AND years 2001 to 2031 with years conflicting with months.

You could ask the user to clarify in certain situations, but that is even more complication.

Don't forget to add:

    $todate = stripslashes(trim($_POST['form-todate']));

To kill any potential SQL injections.

Also here is a date validation:

    function validateDate($date, $format = 'Y-m-d H:i:s') {
        $d = DateTime::createFromFormat($format, $date);
        return $d && $d->format($format) == $date;
    }

function was copied from this answer or php.net

You can pass the input string to this function with various formats until one or more matches (you probably don't want the time in there so take it out).

If there is more than one format matching - query the user to confirm which one.

By the way if you are webifying this there is a great bootstrap js technique I have used at (i think) https://eonasdan.github.io/bootstrap-datetimepicker/

It makes a mini calendar on your input field so the users don't have to type anything - just point and click.

Community
  • 1
  • 1
Blair Lowe
  • 21
  • 3