?
Solved

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

Posted on 2007-08-10
7
Medium Priority
?
4,047 Views
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
0
Comment
Question by:valicon
7 Comments
 
LVL 16

Expert Comment

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

mktime
http://au2.php.net/mktime
0
 
LVL 4

Assisted Solution

by:DrBrainiac
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.
0
 
LVL 12

Assisted Solution

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

SELECT DATE_FORMAT(CURRENT_DATE, '%d-%m-%Y');
------------
10-08-2007

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


--Adrian
0
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

by:
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

-r-
0
 
LVL 12

Author Comment

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

Expert Comment

by:DrBrainiac
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.
0
 
LVL 12

Author Comment

by:valicon
ID: 19849011
Thanks all. :)
0

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