1

In my code I am using the POST method to store users inputted information into my database. Everything inputs correctly, however I do not understand how I can go about adding a date that a user has inputted, and make the variable recognisable to the date type in the table, the code I have been trying so far:

$dateRequired = date("Y-m-d", $dateRequired); $query = "INSERT INTO CustomerRequest (requestDate, dateRequired, collectionDetails, quantity, requestStatus, customerID) VALUES( NOW(),?, ?, ?, ?, ?)"; $statement = $cxn->prepare($query); //bind parameters for markers, where (s = string, i = integer, d = double, b = blob) $statement->bind_param('ssisi', $dateRequired, $details, $quantity, $requestStatus, $customerID); 

The $dateRequired is the variable I am on about, at the moment I get the output of 1970-01-01, and the user inputted value is 04/29/2015. I am obviously doing something wrong but can't figure out which part is the issue, and best way to solve it.

Any ideas on how to get the user entered value for $dateReqired recognizable to the mysql database?

1

1 Answer 1

5

You need to use strtotime() to convert the data to timestamp. date() needs the second parameter be a timestamp value. Try with -

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

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

Sign up to request clarification or add additional context in comments.

4 Comments

Just a thought, strtotime isn't always reliable depending on your input. Would suggest looking at DateTime::createFromFormat and the DateTime format method.
@JonStirling Yes. sometimes depending on the format passed.
@sgtBOSE Well, it is and it isn't. Given the information provided, it isn't strictly, but all that needs to happen is that somebody submits a - separator instead of a / and your dates are wrong or, somebody puts their numbers in the wrong way. It is user input, and strtotime isn't a valid filter. Just pointing out, not saying the answer is wrong ;).
@JonStirling your point is right. For that some validation are needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.