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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.