Jump to content

Converting str date to insert into DB


ShaolinF

Recommended Posts

Hi Guys

 

I am having issues converting my user input date into something understandable by the DB. The user inputs a date with the following format DD/MM/YYYY.

The DB column is set as follows:

course_start_date date

 

How do I convert the user date so that I can insert it into the DB ? I tried strtotime() but that doesnt work.

Link to comment
https://forums.phpfreaks.com/topic/183050-converting-str-date-to-insert-into-db/
Share on other sites

There are two ways you can do this for the format you have shown -

 

1) Use the mysql STR_TO_DATE() function in your query,

2) Explode the string on the / character, then form a new string with the fields in the correct format.

 

Unfortunately, the strtotime() function does not understand DD/MM/YYYY, so you would need to alter the string before you could use strtotime() on it. When using the / for a delimiter, you would need to supply a mm/dd/yyyy. See this link for a definition of what strtotime can parse - http://www.gnu.org/software/tar/manual/html_node/Calendar-date-items.html#SEC116

Thanks. Regarding strtotime() it looks like it does convert the string date but its format isnt being accepted. See error below:

[incorrect date value: '1228867200' for column 'course_start_date' at row 1]

 

STR_TO_DATE() seems to be a viable option. However I don't know how to implement it since I am inserting arrays into the DB so I cant really call the STR_TO_DATE() function.

 

$DataArr = array('course_start_date' => $course_date, 'sometype' => 'value');

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.