How to change date format in MySQL from a PHP form submission

Posted on 2007-08-10
Medium Priority
Last Modified: 2012-06-27
Hi all,

I have a PHP form that submits data to a MySQL database. Users can search for records or add and delete records from their browsers as it is running in PHP. However MySQL date format is in Y-M-D and I need it to be M-D-Y. What would be the easiest way to do this?  I will need help with the code also. Thanks
Question by:valicon
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 19669585
i didnt get it clearly.
anyway check this


Assisted Solution

DrBrainiac earned 500 total points
ID: 19669632
Best way is to save all dates and times as a unix timestamp in you database. Then you can use functions like mktime(0,0,0,$month,$day,$year) (mentioned above) to generate a timestamp, and date("m-d-Y,$timestamp) to convert the timestamp to a readable date in your desired format.
LVL 12

Assisted Solution

AdrianSRU earned 500 total points
ID: 19672257
The DATE_FORMAT function should also work for you:


With this method you can take care of the date formatting in the query instead of leaving it for your PHP code.

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

LVL 49

Accepted Solution

Roonaan earned 1000 total points
ID: 19674925
Personally I would go with
SELECT UNIX_TIMESTAMP(your_date_or_datetimefield) as field FROM table,

That would retrieve a timestamp for your date/datetime field.

The actual output of the date would then be on your output/template part. You'd define a configuration constant containing a site-wide date format, and apply it to the timestamp.
define('DATE_SIMPLE', '%m-%d-%Y')

echo strftime(DATE_SIMPLE, $row['field']);

You'd probably get a warning that your locales need to be set first. This would depend on your php install though

LVL 12

Author Comment

ID: 19764188
Could you give me a few code examples?  I need to know how to work this into my code....

Expert Comment

ID: 19781100
The code of Roonaan is practicly all you need and better then my code. If you post your code, we can help you integrate it.
LVL 12

Author Comment

ID: 19849011
Thanks all. :)

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question