0

I have some problems converting an HTML date to a MySQL date.

I have a form with an input field (type="date"), which outputs a date in format 29/04/2015

Whenever I click on the Send button, I send the date via PHP to a database which has a column in DATE format (2015-04-29)

Unfortunately the result is that the date is always stored as 1970-01-01, which means the conversion didn't work out as expected.

My conversion code, right now, is:

@$entryDate = str_replace("/", "-", $_POST['entryDate']);
@$entryDate = date('Y-m-d', strtotime($entryDate));

Do you have any idea why the conversion is not working?

Thanks a lot for your help

AlexKalopsia
  • 113
  • 2
  • 12
  • 1
    [works for me](https://eval.in/565709). Your error lies elsewhere. And get rid of the `@`. That's just bad. – John Conde May 05 '16 at 00:19
  • uhm that is very odd...I guess then the problem is in the $_POST['entryDate']? To get the date I use Any idea? – AlexKalopsia May 05 '16 at 00:33
  • What does `echo $_POST['entryDate'];` show? FYI, this should have been the first thing you did to debug this. – John Conde May 05 '16 at 00:35
  • Sorry, I'm still learning even the most basic things. The output after `$entryDate = str_replace("/", "-", $_POST['entryDate']);` is `Thu May 05 2016 00:00:00 GMT+0200 (Central Europe Daylight Time)` The output after `$entryDate = date('Y-m-d', strtotime($entryDate));` is `1970-01-01` I would guess it's because the HTML data input is creating a full timestamp? – AlexKalopsia May 05 '16 at 00:47
  • Yes, that date format is not acceptable for strtotime(). See [this Q&A](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) for how to convert any date format. – John Conde May 05 '16 at 00:59

1 Answers1

0

HTML5 is not truly/fully supported in all current browsers.

But if you can step over that, the input method gives you a date formatted in "YYYY-MM-DD" format (regardless of the localization of the display order in the representation to the user in the browser), no need to convert it to use in mysql at all. But do validate it as a valid input, esp. given the lack of support in some browsers (and for once it's not just IE, Firefox also has issues with this one).

See also Is there any way to change input type="date" format?

Community
  • 1
  • 1