Jump to content

Help posting dates from PHP code into MySQL database


t4newman

Recommended Posts

I'm inserting a row of data into a MySQL database and all of the data except two dates correctly get added to the row.  I know what the problem is but I'm not sure of the solution... I'm getting a user to input UK format dates (location of users) but the database is showing 0000-00-00 which I suspect is because it is only recognising US format dates (YYYY-MM-DD).

 

I'm using date_default_timezone_set($zondatetime); and feeding that variable with 'Europe/Paris' but it's not making any difference.

 

Any help very much appreciated.

 

Actually, YYYY-MM-DD is not a US format. It is a left-to-right, most significant (year) to least significant (day), format that allows dates to be compared and sorted correctly.

 

There are several methods that you can use to changed the format from what is being entered into the expected format. Two of the fastest are -

 

1) After you validate the entered date, use explode to break the date into its separate fields, then put the pieces back together in the correct format,

2) After you validate the entered date, use the mysql STR_TO_DATE() function directly in your query to produce a DATE value from the entered value.

 

date_default_timezone_set() only sets the time zone that the php functions like date(), mktime(), strtotime()... use. It has no effect on what your code does with the data it receives.

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.